** 1. Specify the attribute ** in the find method
User::find(1, ['name', 'email']);
User::findOrFail(1, ['name', 'email']);
Copy the code
Clone a Model
You can clone a Model with the replicate method
$user = User::find(1);
$newUser = $user->replicate();
$newUser->save();
Copy the code
3. Determine whether the two models are the same
Check whether the ids of two models are the same using the IS method
$user = User::find(1);
$sameUser = User::find(1);
$diffUser = User::find(2);
$user->is($sameUser); // true
$user->is($diffUser); // false;
Copy the code
Reload a Model
$user = User::find(1); $user->name; // 'Peter' // If name is updated, such as by Peter to John $user->refresh(); $user->name; // JohnCopy the code
5. Load the new Model
$user = App\User::first(); $user->name; // John // $updatedUser = $user->fresh(); $updatedUser->name; // Peter $user->name; // JohnCopy the code
Update the associated Model
All models can be updated using the push method when updating the association
class User extends Model{ public function phone() { return $this->hasOne('App\Phone'); }}$user = User::first(); $user->name = "Peter"; $user->phone->number = '1234567890'; $user->save(); // update User Model $User ->push(); // Update User and Phone modelsCopy the code
7. Customize soft delete fields
Laravel uses deleted_AT as a soft delete field by default, so we changed deleted_AT to IS_deleted in the following way
class User extends Model{ use SoftDeletes; * * @var string */ const deleted_at = 'is_deleted'; }Copy the code
Or use accessors
class User extends Model{
use SoftDeletes;
public function getDeletedAtColumn(){
return 'is_deleted';
}}
Copy the code
Select * from Model
$user = User::first(); $user->name; // John $user->name = 'Peter'; $user->save(); dd($user->getChanges()); / / output: [' name '= >' John ', 'updated_at' = > '... ']Copy the code
9. Query whether the Model has changed
$user = User::first();
$user->name; // John
$user->isDirty(); // false
$user->name = 'Peter';
$user->isDirty(); // true
$user->getDirty(); // ['name' => 'Peter']
$user->save();
$user->isDirty(); // false
Copy the code
GetChanges () differs from getDirty()
The getChanges() method is used to output the result set after the save() method
The getDirty() method is used to print the result set before the save() method
10. Query the Model information before modification
$user = App\User::first();
$user->name; //John
$user->name = "Peter"; //Peter
$user->getOriginal('name'); //John
$user->getOriginal(); //Original
$user record
Copy the code
Pay attention and don’t get lost
All right, everybody, that’s all for this article. All the people here are talented. As I said before, there are many technical points in PHP, because there are too many, it is really difficult to write, you will not read too much after writing, so I have compiled it into PDF and document, if necessary
Click on the code: PHP+ “platform”
As long as you can guarantee your salary to rise a step (constantly updated)
I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc