Monday, 9 September 2013

How do you work with request data in the form of an array in Laravel 4

How do you work with request data in the form of an array in Laravel 4

I'm trying to find the way laravel 4 works with array data in requests but
can't find any example, tutorials, documentation about it on the net.
Example:
<select name="productDetails[0][service]">...</select>
<select name="productDetails[0][product]">...</select>
<select name="productDetails[0][action]">...</select>
Ends up being:
$_GET['productDetails'][0]['service']
$_GET['productDetails'][0]['product']
$_GET['productDetails'][0]['action']
Now if i want to make my form work correctly i have the read this data,
but i can't assume that productDetails is an array and features 0. I want
to use the facilities provided by Laravel/Symfony's HTTP Foundation
components.
I tried without avail:
Request::get('productDetails.0.service', null); //Doesn't work
Request::get('productDetails[0][service]', null); //Doesn't work
I thought these versions would work because you access session data like
this and probably many other features of Laravel work this way, just
stumped that this doesn't work...
On the other hand, this snippet works, but it's ugly cause you can't
assume productDetails is an array at all times, you also can't assume 0
index exists nor "service" index too...
Request::get('productDetails', array())[0]['service']; //Works but ugly
So what is the right way to accomplish this?

No comments:

Post a Comment