Can you show some more? The full class and the query that isn't working as expected as well as any errors
Nov 24, 2022
12
Level 18
Adding a global scope on a table on some pivot table condition
Hi Everyone. I'm a beginner, and I'm trying to create a concept for a multi-user chat app. So I have a many to many relationship for the users and channels table so users can create private chat rooms.
Now I wonted to create a global scope to make sure that users cannot access and see other users channels. So I tried to create something like this.
protected static function booted()
{
static::addGlobalScope('privateChannels', function (Builder $builder) {
$builder->where('user_id', auth()->user->id)
->orWhere('public', true);
});
}}
But it is not working as it cannot access the pivot tables columns. Any thoughts on this?
Level 102
@Dave Wize ok if I understand you correctly
$builder->whereRelation('user', 'id', '=', auth()->user->id)
->orWhere('public', true);
});
1 like
Please or to participate in this conversation.