Laravel: Pass object array as parameter to Controller@Method

时间:2019-01-07 13:30:10

标签: php laravel

So as mentioned in the title I need to call the 'store' method from 'OrdersController' with 2 parameters: $user_id (an integer), and $products(an array of objects).

Here is my code:

<a href="{{ route('store_orders', [$user_id, serialize($products)]) }}" class="btn btn-default">Buy!</a> 

With the route 'store_orders':

Route::get('/orders/{user_id}/{products}', 'OrdersController@store')->name('store_orders');

And the method OrdersController@store:

 public function store($user_id, $products)
{
    //do something 
}

When I try to access the page I get the following error :

Missing required parameters for [Route: store_orders] [URI: orders/{user_id}/{products}].

although I have the 2 parameters.

If i try with '$products' directly, instead of 'serialize($products)' I get the following error:

Array to string conversion

If I pass as the second parameter something else than an array like:

<a href="{{ route('store_orders', [$user_id, $products[0]->name]) }}" class="btn btn-default">Buy!</a>

I don't get the error anymore and everything works fine..

Can you guys give me a solution?

P.S. sorry if I am missing any information or if I have redaction problems. It's my first question on stackoverflow.

0 个答案:

没有答案