1

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

Mr Robot
  • 887
  • 4
  • 18
  • 47
  • decode your json data. – aldrin27 Feb 04 '17 at 05:06
  • Search the php function of `json_decode` – aldrin27 Feb 04 '17 at 05:09
  • How you are calling `update` method and also share what you get in `json_decode($request)` ? – Niklesh Raut Feb 04 '17 at 05:23
  • @Rishi i'm using postman PUT request to test the api, i got `NULL` when i `$request = json_decode($req); dd($request);` – Mr Robot Feb 04 '17 at 05:29
  • @MrRobot what does `dd($request->all());` show? – Alexey Mezenin Feb 04 '17 at 05:30
  • @AlexeyMezenin i'm getting `array:1 [ "data" => array:61 [ "id" => 3 "agent_id" => "201702" "registration_center" => "dfgfsdgdfgdfgfd" "registration_date" => "03-02-2017" "sponsor_name" => "Sponser Name" "event_name" => "Transport Mithra" "registration_id" => "FRTGHY030217001" "profile_photo" => """," "first_name" => "Walter" "last_name" => "White" ] ]` i'm getting all the form values from the POSTman – Mr Robot Feb 04 '17 at 05:34
  • @MrRobot could you please add the dump of `$request->all()` to your question above? It would make it easier to answer and the formatting would be better too. – Vivek Ghaisas Feb 04 '17 at 05:55

2 Answers2

2

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.

Vivek Ghaisas
  • 961
  • 1
  • 9
  • 24
  • thank you for your time i did `dd($request->input('data.registration_center'));` in POSTman i'm getting response `NULL` but if i do `dd($request->all());` i'm getting all the values – Mr Robot Feb 04 '17 at 05:51
  • Okay, let's try to debug this. What do you get when you do `dd($request->input('data'));` ? – Vivek Ghaisas Feb 04 '17 at 05:55
  • Thank you so much for your time really appreciate it, now i'm not storing the data in associative array i'm just sending an JSON object, things are working fine, thank you – Mr Robot Feb 04 '17 at 06:00
  • 1
    Glad I could help! :) – Vivek Ghaisas Feb 04 '17 at 06:01
  • can you please tell me how do i display error if the value did not updated for some reason – Mr Robot Feb 04 '17 at 06:05
1

Since it's an array, you need to access it with:

$request['data']['registration_center'];
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279