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

enc0de's avatar

Making a search page with multiple filters?

Hi, I'm trying to make a search page with multiple filters (checkbox's)

The table i'm trying to search in looks like this:

name body network_id type_id country_id

my search page looks like this:

Select Type

iOS Android

Select Network

YNetworks Some New Network

Select Country America

Germany Australia

I fetch the form data in controller in the form of a array so i'm recving the " the search string "

and the checkboxes input in the form of arrays for eg: type[ 0=>1], network[ 0=>1] and country[ 0=>1,2]

how do i perform this query? if i just use where() or orwhere it fetches everything..

I want it to fetch the content only with the checked boxes, lets say the user selected iOS and Australia i only want to display the posts with iOS and Australia row only and not all the rows with iOS and Australia.

0 likes
4 replies
pmall's avatar

if i just use where() or orwhere it fetches everything..

Please show?

enc0de's avatar

$country = \Request::get('country', ''); $type = \Request::get('type', ''); $network = \Request::get('network', ''); $searchTerm = \Request::get('name', '');

$query = \App\Post::where('name', $searchTerm) ->orwhere('type_id', $type) ->orwhere('network_id', $network) ->orwhere('country_id', $country) ->orderBy('created_at', 'desc') ->get();

this is what my controller looks like atm, the search runs and returns everything that is check , lets say the user selects America from country and 1 type as android.. the result here will be all posts with android and all posts with country. but i only want to return the post which has both america and android only.

and i need it to work on user input, there are 3 checkboxes 1. network which has multiple options 2. type with multiple options and 3. country which has multiple options.

the controller recieves it in arroy form, lets say user checks 2 countrys so $country would contain an array with 0 key = country 1 and key 2 = country 2.. now how do i use this to perform querys on posts table.

Snapey's avatar

I have just done this, and my approach was to use scopes.

The first key is to try and ensure that the value back from the form matches the data in the database.

In my controller, I have

// get topics, filtered by adding local scopes through the Topic class 
        $talks = Topic::with('speaker','tagged','category')    
                        ->region($request->region)
                        ->category($request->category)
                        ->fee($request->fee)
                        ->recency($request->recency)
                        ->orderBy('subject')
                        ->get();

so each of those 'region', 'category','fee' etc are named scopes in the model. I'll show some;

public function scopeCategory($query,$category)
    {
        if(!Empty($category)){
            return $query->where('category_id',$category);
        }
        return $query;
    }

    public function scopeFee($query,$fee)
    {
        if(!Empty($fee)){
            return $query->where('fee_id',$fee);
        }
        return $query;
    }

    public function scopeRecency($query,$recency)
    {
        if(!Empty($recency)){
            $cutoff = Carbon::now()->subDays($recency)->format('Y-m-d');
            return $query->where('updated_at','>',$cutoff);
        }
        return $query;
    }

I hope this gives you some ideas

2 likes
malicemercury's avatar

Hi. how to reach out for another table's column in scope of Model. for ex: i want to reach speaker.name

Please or to participate in this conversation.