02 - Filter Model By user_id: With where() and Global Scope
So now that we have the user_id in our DB table, let's filter by that, so that each user would access only their projects. Option 1
We found 16 results for "global scopes".
So now that we have the user_id in our DB table, let's filter by that, so that each user would access only their projects. Option 1
Now, let's talk about repeating queries in Eloquent. For example, you have a where() condition, and you want to repeat the same condition in other parts of your application and other Controllers, in other classes, and so on
Multi-tenant application are pretty common these days, where database are entries can be accessed only by users who created them. In this article, I will show the simplest way to achieve it in Laravel.
Quite an often situation in real-life projects that you have to order lists by a certain condition. For example, countries/cities in alphabetical order everywhere. Is there a way to avoid adding "orderBy" every time we make a list for a dropdown? Apparently, it's pretty easy - we can use Global Scopes.
Our team is here to help. Ask us anything about Laravel development and we'll get back to you with personalized guidance.
Let me show you a practical example with a few ways how to check if the record belongs to the same team as the logged in user.
Can we make Eloquent global scopes even "more global"? Yes, if we reuse them in multiple models, adding the parameters. Let me show you.
Files in the example:
Files in the example:
Files in the example:
Files in the example:
Files in the example:
Files in the example:
Files in the example:
Files in the example:
Files in the example:
Files in the example: