Reputation: 12005
Now I get POST data in JSON format like:
if ($request->filled('Countries')) {
dd($request->Countries);
}
Where $request->Countries
is array is sent from client:
"Countries": [2,15]
What if I want to send the following JSON format:
{
"places": [{"country": 1, "city": 1}, {"country": 1, "city": 2}]
}
How to get this data using request in Laravel and handle it?
Upvotes: 0
Views: 177
Reputation: 438
just a tip. request()->input() method accepts dot notation which allows you to have unlimited nests. If you want to create a new request from a nested level of your request you could just
$r = $request->input('places');
$requests[] = new Illuminate\Http\Request($r);
Upvotes: 1
Reputation: 12005
For validation nested elements in JSON/Array must have to use this sintax:
$v = Validator::make($request->all(), [
'person.*.id' => 'exists:users.id',
'person.*.name' => 'required:string',
]);
For JSON object:
{"person": {"id": 1, "name": "OOO"}}
Upvotes: -1
Reputation: 2123
use json_decode($json, true) to parse it to a PHP array:
$parsed_array = json_decode($data, true);
Upvotes: 1