How can i use laravel mutator when i have array of input?

45 views Asked by At

Suppose i have this type of input for product

//json
{
    "name": {
        "en": "This is name in English",
        "bn": "This is name in Bangla",
    },
}

now i want to use mutator for generate unique slug based on english name

I'm trying like this

    public function setNameAttribute($value)
    {
        // Generate and set the unique slug
        $this->attributes['slug'] = $this->generateUniqueSlug($value);
    }


1

There are 1 answers

0
reza_qsr On

As far as I understand if you have array of inputs , you should use key to get value. i hope this help you :

public function setNameAttribute($value)
{
      $englishName = $value['en'] ?? null;
      $this->attributes['slug'] = $this->generateUniqueSlug($englishName);
      $this->attributes['name'] = $value;
}