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

deevo's avatar

Multiple Where Clauses from GET Variables

All,

So, I am in a bit of a bind. I have sat and fumbled with a lot of options but nothing seems to be working for me.

Here is what I am trying to do: I have a search form that is searching a "dresses" table. Standard fields like price, length, color, etc. I know I can do multiple where clauses for all of the fields that get passed and then return a paginated collection, but I do not know how to address the issue of when a field does NOT get passed. Say someone chooses not to do a search on the length and is only concerned with the price. How do I handle that GET variable that is not passed or is empty because I found I cannot use an empty string in the ->where() clauses.

Is there a way to make those where clauses dynamic? For example, check to see if a variable is set and then make insert another where clause, or, if it is not set to just skip over it? Is this something I could extract to a class and build up a query to be returned to the controller?

Any advice, and I mean any at all would be greatly appreciated.

R,

0 likes
11 replies
RachidLaasri's avatar
public function search(Request $request)
{
    $dresses = Dresses::query();

    if($request->has('price'))
    {
        $dresses->where('price', $request->get('price'));
    }

    $dresses->paginate(10);

    return view('dresses.index', compact('dresses'));
}
jlrdw's avatar

First of all you don't need all those wheres, these are going to be "ands", also its handled same way as in basic PHP. Refer back to your basic PHP skills and you will figure it out.

deevo's avatar

Unfortunately, this is doing nothing for me. It is just returning some sort of object that cannot be converted to a string. It does enter into the conditional, but I am missing something somewhere that I do not know what it is. I am getting pretty frustrated and disappointed about what I could be doing wrong. Again, any help would be truly appreciated.

public function index(Requests\VestidosSearchRequest $request)
    {
        $search = $request->all();
        if(!empty($search)){
            $vestidos = Vestido::query();
            if($request->has('price'))
            {
                //dd($search['price']);
                $vestidos->where('price', '<', $request->get('price'));
            }
            $vestidos->with('images')->with('user')->paginate(6);
            return view('vestidos/index', compact('vestidos', 'search'));
        }
        $vestidos = Vestido::with('images')->with('user')->paginate(6);
        return view('vestidos/index', compact('vestidos', 'search'));
    }
RachidLaasri's avatar

Try this

public function index(Requests\VestidosSearchRequest $request)
{

    $search = $request->all();
    
    $vestidos = Vestido::with(['images', 'user']);

    if($search->has('price'))
    {
        $vestidos->where('price', '<', $search->get('price'));
    }

    $vestidos->paginate(6);

    return view('vestidos/index', compact('vestidos', 'search'));
}
1 like
deevo's avatar

@RachidLaasri Simple little mistakes always. Had to set the variable equal to the modified object. If you edit your answer to show it, I will definitely select as answered.

public function index(Requests\VestidosSearchRequest $request)
    {
        $search = $request->all();

        if(!empty($search)){

            $vestidos = Vestido::with(['images', 'user']);
            if($request->has('price'))
            {
                $vestidos = $vestidos->where('price', '<', $request->get('price'));
            }

            if($request->has('length'))
            {
                $vestidos = $vestidos->where('length', $request->get('length'));
            }

            $vestidos = $vestidos->paginate(6);
            return view('vestidos/index', compact('vestidos', 'search'));
        }
        $vestidos = Vestido::with('images')->with('user')->paginate(6);
        return view('vestidos/index', compact('vestidos', 'search'));
    }
RachidLaasri's avatar

Is it working now? if yes, you may need to do some refactoring, because there is a lot of repetition on your code.

deevo's avatar

@RachidLaasri for sure I will refactor, I just wanted to get it working. Thank you so much for you help and responsiveness.

martinbean's avatar
Level 80

@deevoweb For simple filtering, I’ve added a scope to my model that I can pass an array of parameters to, and the scope will add the relevant clauses. An example:

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;

class Dress extends Model
{
    public function scopeFilterByRequest($query, Request $request)
    {
        if ($request->has('color')) {
            $query->where('color', '=', $request->get('color'));
        }

        if ($request->has('size')) {
            $query->where('size', '=', $request->get('size'));
        }

        // And so on

        return $query;
    }
}

Usage:

public function index(Request $request)
{
    $dresses = Dress::filterByRequest($request)->paginate();

    return view('dresses.index', compact('dresses'));
}

Hope that helps.

3 likes
deevo's avatar

@martinbean This was so beautiful it almost made me cry. The only thing I did different was built up the query by adding $query = $query->something and just went through all of my GET variables that were passed through. Works like a charm and really cleaned up my controller.

Please or to participate in this conversation.