Home  >  Article  >  PHP Framework  >  Laravel Eloquent skill sharing (example explanation)

Laravel Eloquent skill sharing (example explanation)

不言
不言forward
2019-01-23 10:41:502376browse

This article brings you skills sharing about Laravel Eloquent (examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Laravel is a feature-rich framework. However, you won't be able to find all available features from the official documentation. Here are some features you may not know about.

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();          //原始 $user 记录

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 the specified attribute Whether it 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: Only if you use syncChanges() to save the model or synchronize It only takes effect when updated

4. Define the deleted_at field

By default, Laravel uses the deleted_at field to handle soft deletions. 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 the model and relationship

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

class User extends Model
{
    public function phone()
    {
        return $this->hasOne('App\Phone');
    }
}

$user = User::first();
$user->name = "Peter";

$user->phone->number = '1234567890';

$user->push(); // 这将更新数据库中的用户和电话

6. Reload the model

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

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

// user 表被其他进程修改。 例:数据库又插入一条 “name” 为 “Peter” 的数据。

$updatedUser = $user->fresh();
$updatedUser->name;       // Peter

$user->name;              // John

7. Reload existing model

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

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

// user 表被其他进程修改。例: “name” 被修改为 “Peter” 。

$user->refresh();
$user->name;              // Peter
Note: refresh() will also update the model's associated model data.

8. Check whether the model is 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

9. Clone a model

You can use the replicate() method to copy a model to 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, you can pass in the second parameter Specify the properties to be found.

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

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

The above is the detailed content of Laravel Eloquent skill sharing (example explanation). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete