Laravel is a feature-rich framework. However, you won’t find all the features available in the official documentation. Here are some features you may not know about.

1. Obtain the original attributes

When modifying a Eloquent model record you can retrieve the original properties 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(); / / the original$userrecordCopy the code

2. Check whether the model is 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
Copy the code

You can also check whether a specified property has been modified.

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

3. Obtain the changed properties

Use getChanges() to get the changed properties

$user->getChanges()

//[
     "name"= >"Peter",]Copy the code

Note: This only takes effect if you use syncChanges() to save the model or synchronize updates

4. Definedeleted_atfield

By default, Laravel uses the deleted_AT field to handle soft deletions. You can change it by 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';
}
Copy the code

Or define access

class User extends Model
{
    use SoftDeletes;

    public function getDeletedAtColumn()
    {
        return 'is_deleted'; }}Copy the code

5. Save models and relationships

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

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 databaseCopy the code

6. Reload the model

Reload a model from the database using fresh().

$user = App\User::first();
$user->name; // John // the user table was modified by another process. Example: Insert another data whose name is Peter into the database.$updatedUser = $user->fresh();
$updatedUser->name;       // Peter

$user->name;              // John
Copy the code

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 another process. For example, name is changed to Peter.$user->refresh();
$user->name;              // Peter
Copy the code

Note: Refresh () also updates the model’s associated model data.

8. Check whether the models are the same

Use the is() method to determine whether the 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
Copy the code

9. Clone a model

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

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

$newUser->save();
Copy the code

In 10.find()Method to specify the properties to look for

When using the find() or findOrFail() methods, passing in a second argument specifies the property to look for.

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

$user = App\User::findOrFail(1, ['name'.'age']);
Copy the code

If you find this article helpful, show your liking by liking it. I’d love to hear your thoughts and thoughts on this. You can find me on Twitter.