Laravel 5.3更新方法不起作用(Laravel 5.3 Update method not working)

编程入门 行业动态 更新时间:2024-10-12 03:19:54
Laravel 5.3更新方法不起作用(Laravel 5.3 Update method not working)

我正在研究一个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.

更多推荐

本文发布于:2023-08-07 15:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464733.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   方法   Laravel   method   Update

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!