Laravel Eloquent Model Tips: Selecting Attributes, Cloning, Comparing, Refreshing, Updating Relations, Soft Deletes, and Change Detection
This article presents a collection of practical Laravel Eloquent model techniques, including selecting specific attributes with find, cloning models via replicate, comparing models with is, refreshing and reloading records, updating related models with push, customizing soft‑delete columns, and detecting attribute changes using getChanges, getDirty, and getOriginal.
1. Selecting specific attributes with the find method
Use the second argument of User::find(1, ['name', 'email']); or User::findOrFail(1, ['name', 'email']); to retrieve only the desired columns.
2. Cloning a model
The replicate method creates a copy of an existing model:
$user = User::find(1); $newUser = $user->replicate(); $newUser->save();3. Determining if two models are the same
Use the is method to compare model IDs:
$user = User::find(1); $sameUser = User::find(1); $diffUser = User::find(2); $user->is($sameUser); // true $user->is($diffUser); // false4. Reloading a model
After a database change, call refresh to reload the current instance:
$user = User::find(1); $user->name; // 'Peter' // name updated elsewhere $user->refresh(); $user->name; // 'John'5. Loading a fresh model instance
fresh returns a new instance with the latest data:
$user = App\User::first(); $user->name; // John $updatedUser = $user->fresh(); $updatedUser->name; // Peter $user->name; // John6. Updating related models
Define a relationship and use push to persist changes on both parent and related models:
class User extends Model { public function phone() { return $this->hasOne('App\Phone'); } } $user = User::first(); $user->name = "Peter"; $user->phone->number = '1234567890'; $user->save(); // only updates User $user->push(); // updates User and Phone7. Customizing the soft‑delete column
Override the default deleted_at column by defining a constant or accessor:
class User extends Model { use SoftDeletes; const DELETED_AT = 'is_deleted'; }or
class User extends Model { use SoftDeletes; public function getDeletedAtColumn() { return 'is_deleted'; } }8. Retrieving changed attributes after saving
$user = User::first(); $user->name; // John $user->name = 'Peter'; $user->save(); dd($user->getChanges()); // ['name' => 'John', 'updated_at' => '...']9. Checking if a model is dirty
$user = User::first(); $user->isDirty(); // false $user->name = 'Peter'; $user->isDirty(); // true $user->getDirty(); // ['name' => 'Peter'] $user->save(); $user->isDirty(); // falseThe getChanges() method reports changes after save() , while getDirty() reports pending changes before saving.
10. Accessing original attribute values
$user = App\User::first(); $user->name; // John $user->name = "Peter"; $user->getOriginal('name'); // John $user->getOriginal(); // all original attributesphp中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.