mercredi 5 décembre 2018

Laravel: How to pass an input value to web route

So, I am doing a search functionality. I have created the route and the controller and they both work fine if I just type the link on the search bar of the browser. What I cannot figure out is how to do it with an actual search button.

    {!! Form::open([ 'route' => [ 'platform-management.products.search', $store->slug],
                    'method' => 'GET' ]) !!}
    <div id="imaginary_container"> 
        <div class="input-group stylish-input-group">
            <input type="text" class="form-control" name="search" id="search">
            <span class="input-group-addon">
                <button type="submit">
                    Search
                </button>  
            </span>
        </div>
    {!! Form::close() !!}
    </div>

this is my blade and it looks like this: enter image description here

This is my controller:

public function search_results(Request $request, $store, $search)
{
    $store      = Store::where('slug', $store)->firstOrFail();

        $products = Product::where('name', 'LIKE', "%{$search}%");


    $products   = $products->where('store_id', $store->uuid)
                    ->orderBy('name')
                    ->paginate();

    return view('platform-management.products.search', compact(
        'store',
        'search',
        'products'
    ));
}

my web route is:

Route::get('{store}/products/search/{search}', [
    'as'    => 'products.search',
    'uses'  => 'ProductsController@search_results',
]);

If I type in http://127.0.0.1:8000/platform-management/storename/products/search/sample in the address bar, it works. I can see the search results. But if I try to use the search bar, it redirects to http://127.0.0.1:8000/platform-management/storename/products/search only. the {search} part in the route does not appear. My question is, how can I pass the value of the input to the {search} variable in the route. I tried doing

{!! Form::open([ 'route' => [ 'platform-management.products.search', $store->slug, $request->input('serach')],
                 'method' => 'GET' ]) !!}

and

{!! Form::open([ 'route' => [ 'platform-management.products.search', $store->slug, input('serach')],
                 'method' => 'GET' ]) !!}

of course it won't work haha.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2QiFsG7
via IFTTT

Aucun commentaire:

Enregistrer un commentaire