0

Im using Laravel 7.* and i want to post data as JSON to laravel route.

Due to the Laravel documantation JSON fileds must be accessible using this code :

$name = $request->input('user.name');

this is a pieces of laravel documentation :

When sending JSON requests to your application, you may access the JSON data via the input method as long as the Content-Type header of the request is properly set to application/json.

I'm testing it by RestMan extension in browser and i'm sending data with content-type:application/json Header .

But the $request->input() is empty always.

I tested $request->json(); and this method is working fine . why input() method NOT works for retrive json data ?

Edit :

this is my request in RestMan : enter image description here

And this is my Route :

    Route::post('/get-json-data',function(Request $request){
    return $request->input('user.name');
});
1
  • You should show the request. Does RestMan Export the request to Curl? Could help us to know more Commented May 24, 2020 at 11:33

1 Answer 1

1

Your user parameter in the request is an array of jsons, so you'd need to specify the index to access individual names

$request->input('user.0.name');
$request->input('user.1.name');

or loop through it and get an array of names like this

$names = [];
foreach($request->user as $user){
    $names[] = $user->name;
}

Your original code

$request->input('user.name');

checks would work if user was a json field and name is a field in the json, something like

{
    "user":{
        "name":"Ford"
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

The problem was my understanding of JSON . thanks . problem solved

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.