Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ignaaaam's avatar

How to filter posts by different filters to the URL

Hello i'm doing a project in Livewire 3 using Livewire Volt, my problem is that I have a sub-header which has filters for the posts (you can filter by type, province, town, search term). What i'm doing right now is to dispatch every filter into the grid parent component where posts are rendered. But my goal is to end up building an URL like this once you start selecting filters: /{type}/{province}/{town}/{search-term}. I should do this in the PostController on web routes? Or can be done directly on the livewire component? How should I do that to end up having the url like this?

At the moment what I've done is on the PostController index method if you select a type you go to the url /{type} but the rest of filters are handled on the livewire component via state([])->url() so the url ends up being like this /{type}?selectedProvince=1?selectedTown=1?searchTerm='blabla' but this would affect the SEO? I really want an url like first one mentioned.

0 likes
11 replies
jlrdw's avatar

I'd start with a form so the user can fill these {type}/{province}/{town}/{search-term} and it will be the querystring. A form can use a get request. Or post and thereafter a get.

More than one way to achieve this.

Edit:

Also have a look at laravel scout. But for a "drill down" search really not needed.

but this would affect the SEO?

A query string doesn't affect SEO. SEO is mainly concerned with home page, meaning the main page a visitor is searching for in Google.

For example:

https://laracasts.com/discuss/channels/livewire/how-to-filter-posts-by-different-filters-to-the-url?page=1&replyId=939851

Has nothing to do with the main site:

https://laracasts.com/      // This need SEO, not a search once on the site.
ignaaaam's avatar

I'm a bit confused because in all applications filters are being done with /filter1/filter2/filter3 instead of filtering the url with ?selectedProvince for example and more. So I don't know what would be better. Maybe is it possible to end up with a url like the one I want without redirecting or reloading the page?

jlrdw's avatar
jlrdw
Best Answer
Level 75

@ignaaaam to maintain filters you either pass them in session or pass them along in the redirect.

Just a very basic example:

$dogsearch = !empty(Request::input('psch')) ? Request::input('psch') : '';
return redirect('dog/indexadmin/' . $dogsearch);

But query parameters can be passed in a redirect as well.

An older example where I used session, but you can turn into one variable as well:

return redirect('dog/indexadmin?p=' . Session::get('dogpage') . '&psch=' . Session::get('dogsearch') . '&aval=' . Session::get('dogaval'));

Just a basic search: https://drive.google.com/file/d/0B1_PFw--3o74YjVreHNBOWU2aEE/view?resourcekey=0-LDIGHWAfcPBfmGyYg-O_Aw

Query used:

$query = Dog::where('dogname', 'like', $dogsch);
        if ($aval == "n") {
            $query->where('adopted', '=', 1);
        } else if ($aval == "y") {
            $query->where('adopted', '=', 0);
        }
        $dogs = $query->orderBy('lastedit', 'DESC')->paginate(5);

All of this is just a little logic.

ignaaaam's avatar

@jlrdw Okay I'll follow from here with this instructions, thanks a lot! Will update with more doubts if I get stuck or I do not achieve it. THANKS A LOT!!! ❤️

martinbean's avatar

But my goal is to end up building an URL like this once you start selecting filters: /{type}/{province}/{town}/{search-term}

@ignaaaam And what happens if someone provides a search term only? Or wants to search by town but not province?

You should be using query strings, not path parameters. The clue is also in the name: query strings. They’re used for querying a resource, like you’re wanting.

As for SEO, yes, it will be affected, because if you took the path-based approach in your original post, then search engines like Google would treat those as individual URLs and pages. If you use a query string, search engines will understand you have a page, and that those are different filters for that page and canonicalise (i.e. not care about the order) any parameters.

1 like
martinbean's avatar

@ignaaaam Search engines can handle query string parameters just fine.

You should only be using path parameters for hierarchical data; not for filtering. As if you have four path parameters then you need to specify them in order, and you have to specify each one. You can’t specify just {type} and {search-term} (and omit {province} and {town} which are sandwiched between them).

ignaaaam's avatar

@martinbean Okay will use query parameters for all then, more easier to do it in Livewire and dont think that will affect the SEO that much.

Thanks a lot for the advice sir!

ignaaaam's avatar

@martinbean Finally what I did was to combine query strings with path parameters, since I need the SEO and it will be important, i'll just use query string for the search term. The rest is using path parameters. Thanks a lot for the help! ❤️❤️❤️

ignaaaam's avatar

@jlrdw Yeah sorry sir 😂🙏 too overwhelmed with this project and the learning of Livewire but you're right. Thanks!!! 🔝

martinbean's avatar

Finally what I did was to combine query strings with path parameters, since I need the SEO

@ignaaaam And again, search engines can parse query string parameters just fine. It’s not 2005 any more.

1 like

Please or to participate in this conversation.