1. Check if Model changed
You must keep look or identify if the model has been updated using isDirty()
$user = App\User::first();
$user->isDirty(); //false
$user->name = “Peter”;
$user->isDirty(); //true
If the individual attribute has been changed, you can also check.
$user->isDirty(‘name’); //true
$user->isDirty(‘age’); //false
2. Get original attributes
Once you develop the eloquent record and if you need original attributes, Just call getOriginal()
$user = App\User::first();
$user->name; //John
$user->name = “Peter”; //Peter
$user->getOriginal(‘name’); //John
$user->getOriginal(); //Original $user record
3. Get changed attributes
If you want to undo the changed attributes of a model, do it by using getChanges()
$user->getChanges()
//[
“name” => “Peter”,
]
Note: Here you need to keep in mind that, you will only get changes back if you save it or sync the changes with syncChanges()
4. Custom deleted_at column
In Laravel, the default deleted_at column is used for deleting, if you wish to change it, you need to define the DELETED_AT property.
class User extends Model
{
use SoftDeletes;
* The name of the “deleted at” column.
*
* @var string
*/
const DELETED_AT = ‘is_deleted’;
}
Or by defining an accessor.
class User extends Model
{
use SoftDeletes;
public function getDeletedAtColumn()
{
return ‘is_deleted’;
}
}
5. Save relationships and models
By using the push() method, you can save the relationships and models.
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 both user and phone record in DB
6. Reload fresh model
By using fresh(), you can Reload a fresh model
$user = App\User::first();
$user->name; // John
// user record get updated by another thread. eg: ‘name’ changed to // Peter.
$updatedUser = $user->fresh();
$updatedUser->name; // Peter
$user->name; // John
7. Reload existing model
By using the refresh(), we can reload existing models along with fresh value.
$user = App\User::first();
$user->name; // John
// user record get updated by another thread. eg: ‘name’ changed to // Peter.
$user->refresh();
$user->name; // Peter
8. Check if models are the same
You can identify if the models are same by using is()
$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
With replicate(), you can clone a model. It helps you to create a copy and non-existing model instance.
$user = App\User::find(1);
$newUser = $user->replicate();
$newUser->save();
10. Specify attributes in find() method
Use find() or findOrFail() methods, to identify the attributes for selecting the second argument.
$user = App\User::find(1, [‘name’, ‘age’]);
$user = App\User::findOrFail(1, [‘name’, ‘age’]);
I wish it was interesting and helpful. You can show your love by sharing and commenting. Also, open to sharing the thoughts on this.
Finally…
If you want to utilize the best features of Laravel Eloquent for your Laravel based project. You can find out the best Laravel Development services and Hire Laravel Developer. Experienced and talented developers utilize the best resources and give you the best solution for development.
댓글 없음:
댓글 쓰기