If you can't find a "real" answer to your problem you could use a scope named client
. At least that would make it shorter to write.
Model::client()->get()
public function scopeClient() {
return $query->whereClientId(Auth::user()->id);
}
Thanks. Didn't think about scopes. That makes it cleaner.
You could also add a function in your repository that creates the initial query and applies the scope to it, that way if you need to, for example toggle filtering by client ID on or off, you only need to do so in one location.
protected function newQuery()
{
$query = $this->model->newQuery();
if($this->filterByClientId) {
$query->client($this->currentClientId);
}
return $query;
}
protected function findById($id)
{
return $this->newQuery()->whereId($id)->first();
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community