

The find() method is quite common, but you would be pretty astonished how few people know that it can accept multiple IDs as an array:įor example: $product = Product::find() If needed, you may pass an attribute name to see if a particular attribute was changed: $user = User::create([ The wasChanged method determines if any attributes were changed when the model was last saved within the current request cycle. This method also accepts an optional attribute argument: use App\Models\User The isClean will determine if an attribute has remained unchanged since the model was retrieved. You may pass a specific attribute name to the isDirty method to determine if a particular attribute is dirty. The isDirty method determines if any of the model's attributes have been changed since the model was retrieved. Also saves the relationship related to original modelĮloquent provides the isDirty, isClean, and wasChanged methods to examine the internal state of your model and determine how its attributes have changed from when the model was originally retrieved. Because the push method saves the original model and all of its relationships. For that to happen, you should call the push method. If you call the save method then only the product's price will be saved and not the store name. $product = Product::where('title', 'xyz')->first() The push() method saves the model and all of its relationships. Once your model is saved or retrieved from the database the exists property will be set to true.

When you create a new model instance the exists property will be set to false. The exists property is utilized to show whether the object exists in the database or not. Product::find($product_id)->decrement('stock') Post::find($post_id)->increment('read_count', 10) You can also write it as follows: Post::find($post_id)->increment('read_count') Instead of doing this way: $post= Post::find($post_id) In this article, I will show you a few tricks. Eloquent ORM looks like a simple mechanism, but under the hood, there’s a lot of semi-hidden functions and less-known ways to achieve more with it. In addition to retrieving records from the database table, Eloquent models allow you to insert, update, and delete records from the table as well. When using Eloquent, each database table has a corresponding "Model" that is used to interact with that table. Laravel includes Eloquent, an object-relational mapper ( ORM) that makes it enjoyable to interact with your database.
