Pass array on route Laravel

1

I need to pass an array to a controller. I made the form below but I get the error:

Missing required parameters for [Route: site.add.cart] [URI: adiciona-carrinho/{product}/{option}]. (View: /var/www/html/ecommerce/resources/views/site/pages/product/product.blade.php)

View:

<div class="col-12 offset-lg-6">
    @php
        $array = [1, 2];
    @endphp
    <a id="add-cart" href="{{ route('site.add.cart', ['product' => $product, 'option' => serialize($array)]) }}" class="btn btn-primary mt-5">Adicionar ao Carrinho</a>
</div>

Route:

Route::get('/adiciona-carrinho/{product}/{option}', 'CartController@addCart')->name('site.add.cart');

Controller

public function addCart(Product $product, $option)
{
    dd($option);

}
    
asked by anonymous 09.02.2018 / 02:31

1 answer

1

I was able to pass the array with json_encode () and then using json_decode () in the controller.

<div class="col-12 offset-lg-6">
    @php
        $array = [1, 2];
    @endphp
    <a id="add-cart" href="{{ route('site.add.cart', ['product' => $product, 'option' => json_encode($array)]) }}" class="btn btn-primary mt-5">Adicionar ao Carrinho</a>
</div>

Route:

Route::get('/adiciona-carrinho/{product}/{option}', 'CartController@addCart')->name('site.add.cart');

Controller:

public function addCart(Product $product, $option)
{
    json_decode($option);
    dd($option);
}
    
11.02.2018 / 13:57