clarg18's avatar

Pagination with search query

Controller:

public function search(Request $request)
{
    $stores = Store::with('router', 'tills', 'backOffices','contacts', 'storeStatus', 'storeStatus', 'storeType', 'scos', 'fuelBrand')->where('store_name', 'like', '%' . $request['find'] . '%')->paginate(13);
    return view('store.index')->with([
        'stores' => $stores,
        'find' => $request['find'],
    ]);
}

View

<div class="flex-1 mx-10">
    <form action="/store/search" method="POST" name="search" role="search">
        @csrf
        <input id="index-view-search" name="find" type="text" placeholder="Search"
               class="border rounded w-full p-1"
        >
    </form>
</div>
<div class="mx-3">
    @if ( Route::is('store.index') )
        {{ $stores->links()  }}
    @else
        {!! $stores->appends(['find' => $find])->links() !!}
    @endif
</div>

Route

Route::any('/store/search', "StoreController@search")->name('stores.search');

Can anyone tell me why I get a 404 when I click the pagination links for this code?

The route accepts post or get, I pass back the search term and the page number... /store/search?find=evans&page=2 is what the pagination instance is requesting, I cannot see what is wrong

0 likes
1 reply
clarg18's avatar

I actually figured it out, As I have a resource controller store/{id} is active so I need to rename the URI.

Please or to participate in this conversation.