Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

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);
}
Last updated 1 year ago.
0

Thanks. Didn't think about scopes. That makes it cleaner.

Last updated 1 year ago.
0

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();
}
Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

modbase modbase Joined 31 Jan 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.