10 useful but unknown laravel eloquent features

10 Un-known, But very Useful Laravel Eloquent Features

Spread the love

Laravel is an outstanding framework with rich features, but all available features are not in Laravel documentation. In this post I will highlight some features which are unknown.

01. Specify attributes in find()method.

In Laravel during using find() or findOrFail() methods in second argument you can specify columns.

$user = App\User::find(1, ['name', 'age']);
$user = App\User::findOrFail(1, ['name', 'age']);

02. Clone a model.

You can clone a model using replicate(). It will create a new copy of model.

$user = App\User::find(1);
$newUser = $user->replicate();
$newUser->save();

03. Check if models are same

Check if two models have same ID and they belong to same table using is().

$user = App\User::find(1);
$sameUser = App\User::find(1);
$diffUser = App\User::find(2);
$user->is($sameUser);       // true
$user->is($diffUser);       // false

04. Reload a model.

Reload a model to get updated values from database.

$user = App\User::first();
$user->name;               // John
// user record get updated by another thread. eg: 'name' changed to // Peter.
$user->refresh(); 
$user->name;              // Peter

05. Reload a fresh model.

Reload a fresh model instance from the database using fresh().

$user = App\User::first();
$user->name;               // John
// user record get updated by another thread. eg: 'name' changed to // Peter.
$updatedUser = $user->fresh(); 
$updatedUser->name;       // Peter
$user->name;              // John

06. Save models with relationships.

You can save a model with its relationship using push().

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

07. Create custom deleted_at column.

By default, Laravel handles soft deletes using deleted_at column. You can change it by defining your own custom deleted_at column.

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';
    }
}

08. Get changed attributes of a model.

Get changed attributes of a model by using getChanges().

$user->getChanges()
//[
     "name" => "Peter",
  ]

09. Check if model changed.

Check if a model has been changes using isDirty().

$user = App\User::first();
$user->isDirty();          //false
$user->name = "Peter";
$user->isDirty();          //true

You can also check a specific attribute change.

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

10. Get original attributes.

After mutating Eloquent records you can get the original attributes by using getOriginal().

$user = App\User::first();
$user->name;                   //John
$user->name = "Peter";         //Peter
$user->getOriginal('name');    //John
$user->getOriginal();          //Original $user record

6 thoughts to “10 Un-known, But very Useful Laravel Eloquent Features”

Comments are closed.