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

Melodia's avatar

How to create a search result page with multiple queries

I have a table saves property details, and in the table I store city, suburb, type of property, min and max price, bedrooms and bathrooms.

Now I want to create a search query. If a user only types a city/suburb and leaves the rest of the options blank it should show the search result page according to the properties in that city/suburb. Or the user could type a city and then select type, to give him property type at a chosen city.

Hope you understand what I mean.

So I create this route:

Route::get('/search', 'AdminPropertyController@search_page')->name('search-page');

And this is what I have in the view at the moment

{!! Form::open(['method' => 'GET', 'action'=>'AdminPropertyController@search_page']) !!}
    <div class="search-input">

        {!! Form::text('city', null, ['class'=>'form-controla', 'placeholder'=>'Search for area,city, or suburb']) !!}

        {!! Form::submit('Create User', ['class'=>'btn btn-primary']) !!}
        
     </div>
    <div class="select-boxes">
        
        {!! Form::select('type', ['apartment'=>'Type', 'house'=>'House'], null, ['class'=>'form-']) !!}
        
        {!! Form::select('min_price', ['Min Price'=>'Min Price'] + [1=>'1', 20=>'20'], null, ['class'=>'form-']) !!}
                
        {!! Form::select('maxprice', ['Max Price'=>'Max Price'] + [1=>'1', 20=>'20'], null, ['class'=>'form-']) !!}
    
        {!! Form::select('bedrooms', [''=>'Bedrooms'] + [2=>'1+', 3=>'2+'], null, ['class'=>'form-']) !!}

        {!! Form::select('bathrooms', [''=>'bathrooms'] + [2=>'1+', 3=>'2+'], null, ['class'=>'form-']) !!}
    </div>

{!! Form::close() !!}

I used this in the first input

{!! Form::text('city', null, ['class'=>'form-controla', 'placeholder'=>'Search for area,city, or suburb']) !!}

But actually there I want to search for either city or suburb

At the moment my method looks blank because I have no idea how to do that.

public function search_page(Request $request){
    // to complete
}

Has anyone done anything of that nature?

Hope someone can help

0 likes
9 replies
newbie360's avatar

i don't understand all about the {!! Form::...... !!} because is not documented in 5.6

so i try and guess

public function search_page(Request $request)
{
    // validation ...

    $query = YourModel::query();

    if ($city = $request->input('city') ?? '')
    {
        $query->where('city', $city);
    }

    if ($type = $request->input('type') ?? '')
    {
        $query->where('type', $type);
    }
    
    $result = $query->orderBy('id', 'asc')->paginate(20);
}
1 like
Cronix's avatar

Then you just need to write more logic.

// only add a city query if city is not empty
$city = $request->input('city');
if ($city && ! empty($city))
{
    $query->where('city', $city);
}

and same for the others...

newbie360's avatar

if you means the pagination link showing the &min_price=&maxprice=&bedrooms=&bathrooms= and you don't want it

may be do something like this

public function search_page(Request $request)
{
    // validation ...

    $query = YourModel::query();
    $queryStr = [];
    
    if ($city = $request->input('city') ?? '')
    {
        $query->where('city', $city);
        $queryStr['city'] = $city;
    }

    if ($type = $request->input('type') ?? '')
    {
        $query->where('type', $type);
        $queryStr['type'] = $type;
    }
    
    $result = $query->orderBy('id', 'asc')->paginate(20);
    $result->appends($queryStr);
}
Melodia's avatar

@Cronix Actually I had made a request that didnt exist in the database, hence I could not see anything. So am only left with two things:

First is how to seach for different data in the same input field.Cause I want to search for city and suburb in my first input

Lastly: is there a way to remove the bedrooms from the url if I do not select any bedrooms in the filter?

Thanks in advance

Cronix's avatar
Cronix
Best Answer
Level 67

First is how to seach for different data in the same input field.Cause I want to search for city and suburb in my first input

$city = $request->input('city');
if ($city && ! empty($city))
{
    // search where city = $city or suburb = $city
    $query->where('city', $city)
        ->orWhere('suburb', $city);
}

Lastly: is there a way to remove the bedrooms from the url if I do not select any bedrooms in the filter?

If it's a GET request, not really, and you shouldn't remove them anyway. It's perfectly acceptable to not have values...if there aren't values for it!

1 like
newbie360's avatar

yep as @Cronix say, if use GET method, the variable= in the query string is not affect your backend code, because in your backend code should check the variable

Melodia's avatar

Am back to testing my search results and somethings do look 100% right.

If I seach by suburb only, it works, if I search proerty type it also works, but when try to select min_price or/and max_price, bathrooms and bedrooms, I dont get the desired result.

My updated controller looks like this:

public function search_page(Request $request)
{
    // validation ...
    $query = Property::query();

    if ($city = $request->input('city') ?? '')
    {
        $query->where('city', $city)
    ->orWhere('suburb', $city);
    }

    if ($type = $request->input('type') ?? '')
    {
        $query->where('type', $type);
    }

    // if none of them is null
    if (! (is_null($request->input('min_price')) && is_null($request->input('max_price')))) {
        // fetch all between min & max values
        $query->whereBetween('price', [$request->input('min_price'), $request->input('max_price')]);
    }
    // if just min_price is available (is not null)
    elseif (! is_null($request->input('min_price'))) {
        // fetch all greater than or equal to min_price
        $query->where('price', '>=', $request->input('min_price'));
    }
    // if just max_value is available (is not null)
    elseif (! is_null($request->input('max_price'))) {
        // fetch all lesser than or equal to max_value
        $query->where('price', '<=', $request->input('max_price'));
    }

    $properties = $query->orderBy('id', 'asc')->paginate(9);

    return view('pages.search', compact('properties'));     
}

And below are the search inputs:

{!! Form::open(['method' => 'GET', 'action'=>'AdminPropertyController@search_page']) !!}
    <div class="row flex wrap">

        <div class="col-sm-10">
            <div class="search-input">
                {!! Form::text('city', null, ['class'=>'form-controla', 'placeholder'=>'Search for area,city, or suburb']) !!}          
            </div>

            <div class="select-boxes">
                {!! Form::select('type', [''=>'Type of property'] + ['apartment'=>'Apartment', 'house'=>'House', 'plot'=>'Plot', 'smallholding'=>'Smallholding', 'commercial'=>'Commercial', 'townhouse'=>'Townhouse'], null, ['class'=>'form-']) !!}               
                {!! Form::select('min_price', [''=>'Minimum'] + [1=>'1', 20=>'20'], null, ['class'=>'form-']) !!}                       
                {!! Form::select('maxprice', [''=>'Maximum'] + [1=>'1', 20=>'20'], null, ['class'=>'form-']) !!}
                {!! Form::select('bedrooms', [''=>'Bedroom'] + [2=>'1+', 3=>'2+'], null, ['class'=>'form-']) !!}
                {!! Form::select('bathrooms', [''=>'bathroom'] + [2=>'1+', 3=>'2+'], null, ['class'=>'form-']) !!}
            </div>                                              
        </div>

        <div class="col-sm-2">
            {!! Form::submit('Search', ['class'=>'search bg-brown']) !!}
            <button class="clear-filter button">Clear Filter</button>
        </div>                  
    </div>

{!! Form::close() !!}

Hope someone can help

Please or to participate in this conversation.