我正在研究一个API我有一个资源控制器来CRUD驱动程序,在我的更新功能中我收到此错误
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)我做了dd($request); 在我的更新方法中,我获得了所有JSON响应
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 ] ] }但是对于dd($request->registration_center); 我得到了NULL
我的方法
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'); }我的路线是
Route::group(['prefix' => 'v1', 'middleware' => 'auth:api'], function() { Route::get('user', 'UsersController@index'); Route::resource('drivers', 'DriversController'); //drivers CRUD });期待急需的帮助
谢谢
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
最满意答案
从Laravel请求访问POSTed数据的一般方法是使用input方法。
$field = $request->input('field');看看你的例子,似乎Request对象中有一个data字段,它是一个关联数组,并且registration_center在其中。 如果我做对了,那么在你的情况下,你会使用:
$update_driver->registration_center = $request->input('data')['registration_center']; // or, using Laravel's "dot"-syntax $update_driver->registration_center = $request->input('data.registration_center');访问数据。 对于您尝试检索的所有其他值,也是如此。
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.
更多推荐
发布评论