use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Hash; // In your User Model (App\Models\User) class User extends Model { // --- Accessor Example: Get Full Name --- // Method name: get{AttributeName}Attribute public function getFullNameAttribute() { return "{$this->first_name} {$this->last_name}"; } // --- Accessor Example: Format Creation Date --- public function getCreatedAtFormattedAttribute() { return $this->created_at->format('M d, Y'); } // --- Mutator Example: Hash Password --- // Method name: set{AttributeName}Attribute public function setPasswordAttribute($value) { $this->attributes['password'] = Hash::make($value); } // --- Mutator Example: Capitalize Email --- public function setEmailAttribute($value) { $this->attributes['email'] = strtolower($value); } } // --- Usage Examples --- $user = new User(); $user->first_name = 'John'; $user->last_name = 'Doe'; $user->email = 'JOHN.DOE@EXAMPLE.COM'; $user->password = 'secretPassword123'; // Mutator will hash this $user->save(); // Access the full_name attribute (calls getFullNameAttribute) echo "User Full Name: " . $user->full_name . " "; // Access the formatted creation date (calls getCreatedAtFormattedAttribute) echo "User Created On: " . $user->created_at_formatted . " "; // The email will be stored as 'john.doe@example.com' due to mutator echo "Stored Email: " . $user->email . " "; // The password will be hashed in the database // (You'd typically check it using Hash::check($inputPassword, $user->password)) echo "Stored Hashed Password (for demonstration, not for direct display): " . $user->password . " ";