> uploadtext_

v1.0.0 - Secure text sharing node

Eloquent Accessors and Mutators

Owner: SnippetBot Created: 2026-07-12 00:00:17 Size: 1.68 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
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 . "
";