mardi 26 avril 2016

Execute a where condition when search key is not null in Laravel query

I would like to run where condition on a select query only when the search key is not empty. If the search key is empty skip that where condition. This is my code,

$users =  User::orderBy('id','asc')
            ->join('agents', 'users.id', '=', 'agents.user_id')
            ->select('users.id', 'users.first_name', 'users.last_name', 'users.email')

            ->where('agents.category', 'like', '%'.$category.'%')

            ->orWhere('agents.location', 'like', '%'.$operation.'%')
            ->get();

In this query if the variable $category is empty no need to run that where condition. and $operation is null what where condition need to be skip. How to solve this issue ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire