10 Hidden Laravel Eloquent Features You May Not Know

10 Hidden Laravel Eloquent Features You May Not KnowJino AntonyBlockedUnblockFollowFollowingJan 20Photo by Kristina Flour on UnsplashLaravel is a feature-rich framework.

However, you cannot find all the available features in the official documentation.

Here are some features that you may not know.

Get original attributesAfter mutating an Eloquent record you can get the original attributes by calling getOriginal()$user = AppUser::first();$user->name; //John$user->name = "Peter"; //Peter$user->getOriginal('name'); //John$user->getOriginal(); //Original $user record2.

Check if Model changedDetermine if the model or given attribute have been modified using isDirty()$user = AppUser::first();$user->isDirty(); //false$user->name = "Peter";$user->isDirty(); //trueYou can also check if a particular attribute is changed.

$user->isDirty('name'); //true$user->isDirty('age'); //false3.

Get changed attributesRetrieve the changed attributes of a model using getChanges()$user->getChanges()//[ "name" => "Peter", ]Note: Changes will reflect only if you save the model or sync the changes using syncChanges()4.

Custom deleted_at columnBy default, Laravel handles soft deletes using deleted_at column.

You can change this by explicitly defining the DELETED_AT property.

class User extends Model{ use SoftDeletes; * The name of the "deleted at" column.

* * @var string */ const DELETED_AT = 'is_deleted';}Or by defining an accessor.

class User extends Model{ use SoftDeletes; public function getDeletedAtColumn() { return 'is_deleted'; }}5.

Save models and relationshipsYou can save a model and its corresponding relationships using the push() method.

class User extends Model{ public function phone() { return $this->hasOne('AppPhone'); }}$user = User::first();$user->name = "Peter";$user->phone->number = '1234567890';$user->push(); // This will update both user and phone record in DB6.

Reload fresh modelReload a fresh model instance from the database using fresh()$user = AppUser::first();$user->name; // John// user record get updated by another thread.

eg: 'name' changed to // Peter.

$updatedUser = $user->fresh(); $updatedUser->name; // Peter$user->name; // John7.

Reload existing modelYou can reload an existing model with fresh values from database using refresh()$user = AppUser::first();$user->name; // John// user record get updated by another thread.

eg: 'name' changed to // Peter.

$user->refresh(); $user->name; // PeterNote: refresh() will also update the loaded relations of the existing model.

8.

Check if models are the sameDetermine if two models have the same ID and belong to the same table using is()$user = AppUser::find(1);$sameUser = AppUser::find(1);$diffUser = AppUser::find(2);$user->is($sameUser); // true$user->is($diffUser); // false9.

Clone a modelYou can clone a model using replicate().

It will create a copy of the model into a new, non-existing instance.

$user = AppUser::find(1);$newUser = $user->replicate();$newUser->save();10.

Specify attributes in find() methodWhen using find() or findOrFail() methods you can specify the attributes to select as the second argument.

$user = AppUser::find(1, ['name', 'age']);$user = AppUser::findOrFail(1, ['name', 'age']);If you find this article helpful show some love by clicking on the ????.icon several times.

Also love to hear your opinions and thoughts on this.

You can find me on Twitter.

.

. More details

Leave a Reply