简体   繁体   中英

Laravel 5.3 Update method not working

i'm working on an API i have a resource controller to CRUD the drivers, in my update functon i'm getting this error

QueryException in Connection.php line 770:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'registration_center' cannot be null (SQL: update `drivers` set `registration_center` = , `registration_date` = 04-02-2017, `sponsor_name` = , `event_name` = , `registration_id` = 040217001, `profile_photo` = , `first_name` = , `last_name` = ,`updated_at` = 2017-02-04 04:54:54 where `id` = 3)

i did dd($request); in my update method i got all the JSON response

Request {#40
  #json: ParameterBag {#32
    #parameters: array:1 [
      "data" => array:61 [
        "id" => 3
        "agent_id" => "201705"
        "registration_center" => "dfgsdfgdfgdsdfgdf"
        "registration_date" => "03-02-2017"
        "sponsor_name" => "Sponser Name"
        "event_name" => "RC"
        "registration_id" => "FRTGHY030217001"
        "profile_photo" => ""","
        "first_name" => "Walter"
        "last_name" => "White"
        "created_at" => "2017-01-24 10:08:42"
        "updated_at" => "2017-02-03 11:33:52"
        "deleted_at" => null
      ]
    ]
  }

but for dd($request->registration_center); i'm getting NULL

My Method

public function update(Request $request, $id)
{
    $update_driver = Driver::find($id);

    $update_driver->registration_center = $request->registration_center;
    $update_driver->registration_date = $request->registration_date;
    $update_driver->sponsor_name = $request->sponsor_name;
    $update_driver->event_name = $request->event_name;
    $update_driver->registration_id = $request->registration_id;
    $update_driver->profile_photo = $request->profile_photo;
    $update_driver->first_name = $request->first_name;
    $update_driver->last_name = $request->last_name;                        
    $update_driver->save();

    return $this->respondUpdated('Driver updated successfully');
}

My Routes are

Route::group(['prefix' => 'v1', 'middleware' => 'auth:api'], function() {
    Route::get('user', 'UsersController@index');
    Route::resource('drivers', 'DriversController'); //drivers CRUD
});

Looking forward for much needed help

thank you

The general way to access POSTed data from a Laravel request is to use the input method.

$field = $request->input('field');

Looking at your example, it seems like the Request object has a data field in it which is an associative array and the registration_center is inside that. If I got that right, then in your case, you would use:

$update_driver->registration_center = $request->input('data')['registration_center'];

// or, using Laravel's "dot"-syntax
$update_driver->registration_center = $request->input('data.registration_center');

to access the data. Similarly for all the other values you are trying to retrieve.

由于它是一个数组,您需要访问它:

$request['data']['registration_center'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM