Skills sharing of Laravel Eloquent (example explanation)

01-28-2024

The content of this article is about Laravel Eloquent's skill sharing (example explanation), which has certain reference value. Friends who need it can refer to it and hope to help you. Laravel is a functional framework. However, you can't find all the available functions from the official documents. Here are some features you may not know.

1. Get the original attributes

When modifying an Eloquent model record, you can get the original attributes of the record by calling the getOriginal () method.

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

2. Check whether the model has been modified.

Use the isDirty () method to determine whether the model or a given attribute has been modified.

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

You can also check whether the specified attribute has been modified.

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

3. Get the changed properties

Use getChanges () to get the changed properties.

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

Note: It only takes effect when you use syncChanges () to save the model or synchronize the update. 4. Define the deleted_at field.

By default, Laravel uses the deleted_at field to handle soft deletion. You can change it by defining the DELETED_AT attribute.

class User extends Model { use SoftDeletes; * The name of the "deleted at" column. * * @var string */ const DELETED_AT = 'is_deleted'; }

Or define access.

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

5. Save models and relationships

You can use the push () method to save the model and its association.

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 the users and phones in the database.

6. Reload the model

Use fresh () to reload a model from the database.

$user = App\User::first(); $user->name; // John //The user table was modified by other processes. Example: another piece of data name Peter is inserted into the database. $updatedUser = $user->fresh(); $updatedUser->name; // Peter $user->name; // John

7. Reload the existing model

You can use the refresh () method to reload existing models with new values from the database.

$user = App\User::first(); $user->name; // John //The user table was modified by other processes. Example: the name was changed to Peter. $user->refresh(); $user->name; // Peter

Note: refresh () will also update the associated model data of the model. 8. Check whether the models are the same.

Use the is () method to determine whether two models have the same primary key and belong to the same table.

$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

You can use the replicate () method to copy a model into a new object.

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

10. Specify the search attribute in the find () method.

When using the find () or findOrFail () method, the second parameter passed in can specify the attribute to be found.

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

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us