Jacky BIZELDEAU
Jacky BIZELDEAU

Reputation: 31

Laravel 10 - mutators and accessors don't work

I have a problem with the accessors and mutators in laravel 10. In my first project, they work, but in the second, they don't.

Model

protected function name(): Attribute
{
    return Attribute::make(
        get: fn(string $value) => strtoupper($value),
        set: fn(string $value) => strtolower($value),
    );
}

Controller

public function store(StoreRoleRequest $request)
{
    Role::create([
        'name' => $request->name,
        'guard_name' => "web",
    ]);
    
    return $this->index();
}

The store function and index function work but the mutator and accessor are not triggered. I verified the table field name and tried to store it without a store request.

Upvotes: 3

Views: 1016

Answers (1)

yagnik devani
yagnik devani

Reputation: 103

please try it's working with laravel 10

use Illuminate\Database\Eloquent\Casts\Attribute;

public function categoryImage(): Attribute
{
    return new Attribute(
        
        get: fn ($value) => URL::to('/').'/'.$value
    );
} 

Upvotes: -3

Related Questions