![]() ![]() If the user exists, we retrieve that user. In the example above, we’re querying the “users” table for a user with the specified email. $user = User::where('email', '=', $email)->firstOrCreate([ In such cases, we can use the “firstOrCreate” method in Laravel Eloquent. First OR Create Sometimes we may want to retrieve a record if it exists, or create a new record if it doesn’t.If the user does not exist, a “ModelNotFoundException” exception will be thrown. In the example above, we’re retrieving a user by their primary key. For this, we can use the “findOrFail” method. In some cases, we may want to throw an exception instead. However, if the record does not exist, it will return null. Find OR Fail The “find” method in Laravel Eloquent is commonly used to retrieve a record by its primary key.In the example above, we’re querying the “users” table for all users who are either currently active or who have logged in within the last six months. In such cases, we can use the “orWhere” method in Laravel Eloquent. X OR Y Sometimes we may want to query a database table using two different conditions, where only one of them needs to be true to retrieve the results.Hope you like this tutorial do share and like my tutorials and motivate me to write some awesome tutorials like this, Thank you. # find active users with role admin $users = User :: active () -> withRole ( 'admin' ) -> all () In order to use dynamic model scope in Laravel you can check out following example: How to use dynamic scopes in Laravel model? where ( 'active', 1 ) } public function scopeDelete ( $query ) In this case instead of defining global scope you can add local scope in your Laravel model like following: Local scopes are prefixed using Scope keyword. Let say you do not like above approach and you do not prefer to apply global scope rather you want to apply local scope whenever necessary surely you can do that in Laravel. User :: withoutGlobalScope ( 'delete' ) -> all () # fetch all users weather they are active or not $users = User :: withoutGlobalScope ( new HasActiveScope ) -> all () # fetch active users weather they are deleted or not In that case you can remove your applied scope using following example: For example, if you want to fetch all users weather they are active or not. In some cases, you might not want your global filter to apply. Select * from `users` where `active` = 1 and `deleted` = 0 # fetching all active and non-deleted users $users = User :: all () # above query will result in following sql ![]() These global scope are automatically applied to your model queries. Once your global scopes are added into booted function you do not need to worry about using them. The addGlobalScope method accepts an instance of your scope as its only argument: To assign a global scope to a model, you should override the model's booted method and invoke the model's addGlobalScope method. We would learn how that works later in this tutorial. Whenever laravel calls this scope class it will call apply method and add conditions defined in this method on top of your eloquent query. Let's create a new class under app/Scopes/HasActiveScope.php file with following contents: Global scope is basically a class that allows you to add query filter on top of your all queries that runs through your given model. In this tutorial we would cover following topics: Let say that you have a user model and it has active flag in your table.Įverytime you run any query on User model you want to make sure active where clause is added to our query so that we do not run this query for non-active users. Let's take one example to understand why we need global scope. In this tutorial we would learn about global scopes in Laravel model. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |