Image for post
Image for post

10 Eloquent Features That You Might Not Know About

Daan
Daan
May 31, 2019 · 3 min read

With 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 names

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

2. The exists property

$user = new User;
$user->name = 'George';
$user->email = 'george@example.com';
$user->password = 'secret';
$user->exists; // false$user->save();$user->exists; // true

3. 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. Original

$user = User::first();
$user->name = 'James';

$user->getOriginal(); // Array with "name" => "George"
$user->getOriginal('name'); // "George"

5. Cloning a model

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

6. Convert a model or collection to 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. Refresh

$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 events

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

9. Push

$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 model

$user = User::first();$user->forceDelete();

These 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 as well. 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.

The Startup

Medium's largest active publication, followed by +719K people. Follow to join our community.

Daan

Written by

Daan

Backend developer from The Netherlands. Crypto enthusiast.

The Startup

Medium's largest active publication, followed by +719K people. Follow to join our community.

Daan

Written by

Daan

Backend developer from The Netherlands. Crypto enthusiast.

The Startup

Medium's largest active publication, followed by +719K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store