2

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.

Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
  • Code looks ok; could it be a cache issue? Check the database record to see how it's stored. Also check in `artisan tinker` with simple code like `Role::first()->name` and check if it's being altered. – miken32 Jun 05 '23 at 18:05

1 Answers1

-2

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
    );
} 
yagnik devani
  • 97
  • 1
  • 7