mercredi 12 février 2020

How to update user through api in laravel

I'm trying to update user through api, and this is the function.

PUT request http://20a11140.ngrok.io/api/userregister/24 for user with id 24 for example. I'm using postman for testing When I pass the edited details, with name inclusive public function update(Request $request, $id) { $validator = Validator::make($request->all(), [ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'password' => 'string|min:6|confirmed', 'phone' => 'string|min:6', 'Age' => 'string', 'Blood' => 'string', 'Gender' => 'string', 'Height' => 'string', 'Weight' => 'string', 'record' => 'string' ]);

        if($validator->fails()){
                return response()->json($validator->errors()->toJson(), 400);
        }

        $doc = User::find($id);

        if($request->hasFile('picture')){
            // Get filename with the extension
            $filenameWithExt = $request->file('picture')->getClientOriginalName();
            // Get just filename
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            // Get just ext
            $extension = $request->file('picture')->getClientOriginalExtension();
            // Filename to store
            $fileNameToStore= $filename.'_'.time().'.'.$extension;
            // Upload Image
            $path = $request->file('picture')->storeAs('public/images', $fileNameToStore);
        } else {
            $fileNameToStore = 'noimage.jpg';
        }

        $doc->name = $request->input('name');
          $doc->email = $request->input('email');
          $doc->phone = $request->input('phone');
          if($request->hasFile('picture')){
            $doc->picture = $fileNameToStore;
            }



           $doc->save();

        return response()->json([
            'message' => 'Success',

        ]);

    }

When I pass the edited details, with name inclusive and email, and every required info When I run this code, I get this

"{\"name\":[\"The name field is required.\"],\"email\":[\"The email field is required.\"]}"

What is the issue here?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Hggq3m
via IFTTT

Aucun commentaire:

Enregistrer un commentaire