10 Eloquent Features That You Might Not Know About

10 Eloquent Features That You Might Not Know AboutDaanBlockedUnblockFollowFollowingMay 30With all the features that are out-of-the-box available in Laravel it is hard to know them all.

Some features are not even properly documented.

In this article I will be giving you ten Eloquent features that you might not know about.

1.

Custom timestamp column namesBy default Laravel models have a created_at and an updated_at timestamp.

You can overwrite these column names by defining a constant variable in your model.

If your model uses soft deletes you can overwrite the deleted_at column name aswell.

class User extends Model{ const CREATED_AT = 'created'; const UPDATED_AT = 'last_update'; const DELETED_AT = 'removed';}2.

The exists propertyThe exists property tells whether the object exists in the database or not.

When you create a new model instance the exists property will be set to false.

Once your model is saved or retrieved from the database the exists property will be set to true.

$user = new User;$user->name = 'George';$user->email = 'george@example.

com';$user->password = 'secret';$user->exists; // false$user->save();$user->exists; // true3.

DirtyTo check if a model has been edited since it was retrieved from the database you can use the isDirty method.

It is good to know that when a model isn’t saved at all the isDirty method will return true.

This method has an optional parameter that makes it able to check if a specific property is dirty.

You can use the getDirty method to get an array of all the propterties that are dirty on a model instance.

$user = User::first();$user->isDirty(); // false$user->name = 'James';$user->isDirty(); // true$user->isDirty('email'); // false$user->isDirty('name'); // true$user->getDirty(); // ["name" => "James"]4.

OriginalGetting a model’s original attribute values can be done by using the getOriginal method.

This method has an optional parameter that makes it able to get the original value of a specific property.

$user = User::first();$user->name = 'James';$user->getOriginal(); // Array with "name" => "George"$user->getOriginal('name'); // "George"5.

Cloning a modelCloning a model is a piece of cake.

It can be done by calling the replicate method on your model instance.

$user = User::first();$clonedUser = $user->replicate();6.

Convert a model or collection to arrayThe toArray can convert a model or collection to a plain PHP array.

// Convert a model instance to an array$user = User::first();$user->toArray();// Convert a collection to an array$users = User::all();$users->toArray();7.

RefreshThe refresh method will refresh the model by getting fresh data from the database.

All of its loaded relationships will be refreshed aswell.

$user= User::where('name', 'George')->first();$user->name= 'James';$user->address->city = "New York";$user->refresh();$user->name; // "George"$user->address->city; // "Washington"8.

Without eventsSometimes you want to create or update a model without firing any events.

In Laravel it is possible to execute a callback without firing any model events for any model type.

$user = User::withoutEvents(function () { return factory(User::class)->create(); });9.

PushThe push method saves the model and all of its relationships.

$user = User::where('name', 'George')->first();$user->age = 42;$user->address->city = "New York"; If you just call the save method on the user the address won’t be saved.

$user->save();By using the push method both the user and the address will be saved.

$user->push();10.

Force a delete on a soft delete modelIn some situations you want to remove a soft deleted model from your database.

This can be done by using the forceDelete method.

$user = User::first();$user->forceDelete();This are the ten Eloquent features that I wanted to share with you.

Please share your thoughts on this article.

Make sure to check out my other posts aswell.

A lot of my content is about Laravel.

Feel free to leave a comment if you have any feedback, questions or want me to write about another Laravel related topic.

.

. More details

Leave a Reply