Top 12 Hidden Laravel Eloquent Features

The Object Relational Model (ORM) implements a pattern known as Active Record pattern which offers a smooth way to interact with your database. Each model represents a table in your database with which you can work.

Let us go through the best 12 hidden Laravel eloquent features to improve your code.

1. Get original attributes

To execute the get original attributes, simply call getOriginal() and you’re done. Here is it:

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

2. Check if the Model changed

Check the attribute using isDirty() method and it will look like:

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

Check if any particular attribute is changed or not.

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

3. Get changed attributes

Now, retrieve the modified attributes using getChanges().

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

4. Custom deleted_at column

Laravel also handles soft deletes using the delete_at column like:

class User extends Model
{
   use SoftDeletes;
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 relationships

Save the corresponding relationships and all the application models using the push() method.

class User extends Model
{   public function phone()
   {
     return $this->hasOne('App\Phone');
   }
}$user = User::first();
$user->name = "John";$user->phone->number = '1010101010';$user->push();

6. Reload fresh model

Using the fresh() method, reload a fresh model from the database.

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

7. Reload existing model

Reload the existing model from the database using refresh()

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

8. Check if models are the same

Check if both the models have the same ID 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

9. Clone a model

Clone the database’s model using replicate() method

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

10. Specify attributes in find() method

Using find() method, you can specify the attributes as your second argument.

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

11. Expressive Where Syntax

You can enhance where clause and make it more expressive while retrieving a particular record. Usually when you grab a record, you would do something like this:

$product = Product::where('category', 2)->get();

12. Increment & Decrement Model Attributes

Using increment() and decrement() methods you can set the increment or decrement model attribute, as shown below:

// adds one view to product model
Product::find($productId)->increment('views');
// or you can add any number
Product::find($productId)->increment('views', 5);
// Remove one view from product model
Product::find($productId)->decrement('views');
// or you can add any number
Product::find($productId)->decrement('views', 5);

Start a project today!

Fill up the form and our team will get back to you within 24 hours.

Mail us [email protected]
Or call +30 210 300 1787
             +30 2311 111 901