Home >PHP Framework >Laravel >Avoid the trap! Laravel update data pitfalls

Avoid the trap! Laravel update data pitfalls

藏色散人
藏色散人forward
2020-11-23 14:50:169529browse

The following is the Laravel framework tutorial column to share with you the pitfalls of Laravel update data, I hope it will be helpful to friends in need!

Avoid the trap! Laravel update data pitfalls

Usually we have the following methods to update data:

Method 1

$model = Model::find($id);$model->field1 = $value1;$model->field2 = $value2;$model->save();

Method 2

Model::find($id)
    ->update([
        'field1' => $value1,
        'field2' => $value2,
    ]);

Method 3

Model::query()
    ->where('id', $id)
    ->update([
        'field1' => $value1,
        'field2' => $value2,
    ]);

All three methods can update data. If it were you, which method would you choose? Or which method has the best performance?                                                                                                                     

The above is the detailed content of Avoid the trap! Laravel update data pitfalls. For more information, please follow other related articles on the PHP Chinese website!

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