I would use authorization to see if the user is blocked instead of doing the check in my query.
You can for example use a Gate for it
I have an e-commerce app using Laravel multi-stores I have a feature that stores can block specific user
the structure of DB:
stores table:
id - name - etc ...
users table:
id - name - etc ...
products table:
id - name - store_id - etc ...
blocked_users table: "this table contains users that store blocked"
id - user_id - store_id - created_at - updated_at
I need when a store block a user the products of this store does not show to this blocked user
my query:
$products = Product::query()
->with(['store.blocked' => function ($q) {
$q->where('user_id', '!=', auth()->id());
}])
->whereHas('store.blocked', function ($q) {
$q->where('user_id', '!=', auth()->id());
})
->take(12)
->get();
With this query, I got no products at all!
Blocked is a relation in the Store model:
public function blocked()
{
return $this->hasMany(BlockedUsers::class);
// BlockedUsers model of "blocked_users" table
}
My guess is that you have to reverse it
$products = Product::query()
->with(['store.blocked' => function ($q) {
$q->where('user_id', '!=', auth()->id());
}])
->whereDoesntHave('store.blocked', function ($q) {
$q->where('user_id', '=', auth()->id());
})
->take(12)
->get();
Please or to participate in this conversation.