An empty variable will run a query that looks something like this:
SELECT * FROM `users` WHERE `name` = "%%" OR `email` LIKE "%me@example.com%" OR `city` LIKE "%adelaide%"
This becomes a problem because your wildcard is searching for a name (or email or city) that is anything.
What you could do is set yourself up a query scope in your user model:
class User extends Eloquent {
// ...
public function scopeFilter($query, $params)
{
if ( isset($params['name'] && trim($params['name']) !== '' )
{
$query->orWhere('name', 'LIKE', "%{$params['name']}%");
}
if ( isset($params['email'] && trim($params['email']) !== '' )
{
$query->orWhere('email', 'LIKE', "%{$params['email']}%");
}
if ( isset($params['city'] && trim($params['city']) !== '' )
{
$query->orWhere('city', 'LIKE', "%{$params['city']}%");
}
return $query;
}
// ...
}
Then in your controller / repository you can do something like:
$params = Input::only('name', 'email', 'city');
$users = Users::filter($params)->get();
>>> User::filter([ 'name' => 'testing', 'city' => 'adelaide', ])->get()->toArray();;
=> [
[
"id" => "1",
"name" => "deringer",
"email" => "deringer@example.com",
"created_at" => "2015-02-10 08:44:56",
"updated_at" => "2015-02-10 08:44:56",
"city" => "Adelaide"
]
]
Using a scope means that you can reuse the filtering logic anywhere in the site, you can add additional filter parameters or tweak the behaviour of it in one place.
Let me know how you go.