dimanche 9 février 2020

Why do I get the name field is required, while trying to update through api in laravel, it doesn't seem to be taking my inputs

This is my update function, I'm using postman to test the api, My url goes: http://localhost:8000/api/userregister/25 25 is the ID of the user I'm trying to update. I'm sending a PUT request

I get this response:

Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (SQL: update users set name = ?, email = ?, phone = ?, users.updated_at = 2020-02-09 16:39:43 where id = 25) in file C:\xampp\htdocs\doc\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 669

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' => 'required|string|min:6|confirmed',
    //     'phone' => 'required|string|min:6',
    //     'Age' => 'required|string',
    //     'Blood' => 'string',
    //     'Gender' => 'required|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 = $bannerName;
        }



       $doc->save();

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

    ]);

}


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

Aucun commentaire:

Enregistrer un commentaire