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

littlebox's avatar

paginate and select,options

Hello. Whole day I try to filter by categories in pagination. I read all of google' topics and nothing. So anybody can help me ?

I'am new and I learn what I see in web (btw),so I want to display posts with a specific category, which can be may. I have select with multiple options:

cat1 cat2

after submit in get I have: myweb.app/x?category=cat1&category=cat2#

eg. I selected cat1 and cat2, after submit I want all posts from these categories How to do that ?

0 likes
4 replies
topvillas's avatar

That's not going to work. Only the second category will be picked up.

Make the categories a comma separated list and explode them in your controller.

Then use a whereIn on your query.

myweb.app/x?categories=1,2,3
$categories = explode(',', $request->get('categories'));

$posts = Post::whereIn('category', $categories)->get();
littlebox's avatar

It sounds smart, but in select can I do that ? Or maybe I have to use js or something else ? I mean how can I separate values with a comma ?

topvillas's avatar
Level 46

There is another way but it will only work if you use post and not get (I think) but try it out with both.

<select name="categories[]" multiple="multiple">
    <option id="1">One</option>
    </option id=2">Two</option>
</select>

Note the brackets after the select's name.

Then PHP will treat the categories as an array and you won't have to bother exploding anything.

littlebox's avatar

Thank u! Whole day and it works! when I used array in html next I used this: $posts = Post::whereIn('category', request()->get('category')), but do you know how to get rid of the "bushes" in url ? %5B%5D = [] ?

Please or to participate in this conversation.