I
I
igor11122019-03-01 05:50:49
Laravel
igor1112, 2019-03-01 05:50:49

Search filter based on GET parameters in laravel?

While implementing a filter on the search page based on GET parameters, I ran into the problem of generating a url address for the filter buttons. In order to save the current search parameters, I do this:

<a href="/search?{{($request->q ? "q=$request->q" : '') . ($request->sort ? "&sort=$request->sort" : '') . "&section=photoreports"}}">Фоторепортажи</a>

How would I do this using a named route, as is customary. There are a lot of such buttons on the page, it looks ugly, poorly supported, and if the route, for example, changes, then everything will break.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2019-03-01
@igor1112

One of the options:

route(
  'route.name',
  array_merge(
      request()->query(),
      [
          'sortBy' => 'relevance_score',
          'sortOrder' => ($currentSortBy === 'relevance_score' && $currentSortOrder === 'asc') ? 'desc' : 'asc',
      ]
   )
)

It seems that the request object has something more beautiful, I don’t remember why we wrote it that way at home.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question