Doubt of structure using Global Scopes in Laravel

Asked

Viewed 38 times

-1

A doubt of structure.

Scenario: I have 3 users: administrator, stores, customers.

I have a product model and I want to define a global Cope in it so that when stores access the product listing the query already brings the products from this store (without me having to spend Where on hand in all listings).

But if I set this global Cope in my product model will give off when I try to access the general product listing when logged in to the administrator.

What’s the best way to do it? I put an if in globalscope checking which user is accessing to know whether or not I should use this globalscope, or I create 2 models of products and use them separately for each user?

And a second question, if some of my functions even in the store user is not necessary to use the global Cope, what do I do? (knowing that most functions are necessary to use).

1 answer

1


If the products will always be filtered by the store id, then I believe that the broker would put a global scope even... as the general listing is only in one place, in this query you use the withoutGlobalScope(). Would something like this:

Product::withoutGlobalScope(StoreScope::class)->get();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.