Here is a sample database-table (users
):
id - int(11) auto_increment
name - varchar(100)
banned - int(1)
The column banned
is a boolean value which is 0
(false
) by default. If an user has been banned, the value is 1
.
I'd like to exclude any banned users from all queries by default. I could create a query scope and then use that everywhere. However, I'd much more prefer simply having that check on by default.
I could also create a newQuery
-method of my own, like so:
// Inside User-model, which extends Eloquent
public function newQuery($excludeDeleted = true)
{
$builder = parent::newQuery($exludeDeleted);
$builder->where('banned', '=', '0');
return $builder;
}
However, this way I wouldn't be able to switch off this behaviour. I might want to see the banned users in my private admin panel, but would not be able to, as this restriction would be applied to any query done via Eloquent.
Any idea on how to solve this problem?