A way to return empty query in Laravel local scopes

Viewed 243

I'm using local scopes in my models in my app to 'filter' allowed models on the index page. If the user doesn't have permission to index models, that scope should return an empty query, and at the ending, it should return an empty result.

public function scopeViewable($query, User $user)
{
    if ($user->can('bills.index'))
    {
        return $query->whereIn('location_id', $user->shopAccess->location_ids);
    }

    // return empty query
}

Is there a more efficient way to return empty queries than

return $query->where('id', null)

Because this model has id as the primary key, it cannot be null, and this will return an empty query, but it can be pretty inefficient. Also, is there something better to limit indexing?

0 Answers
Related