0

How to assign value to my custom property ? I am comming from Yii2 background and there is pretty straight forward. Here I made this:

protected $appends = ['sites'];
    public function getSitesAttribute()
        {
            return $this->sites;
        }
    
        public function setSitesAttribute($value)
        {
            $this->attributes['sites'] = $value;
        }

Trying to assign like mymodel->sites = ['1', '2', '3'] but without success. After trying to get the mymodel->sites value error is thrown: undefined property 'sites'.

1
  • Check public function getSitesAttribute(){ dd($this->attributes['sites']); } Commented Aug 19, 2020 at 18:46

2 Answers 2

1

Your accessor is not quite correct, note that you're not returning $this->attributes['sites']; but instead $this->sites.

class MyModel extends Model
{
    public function getSitesAttribute()
    {
        return $this->attributes['sites'];
    }

    public function setSitesAttribute($value)
    {
        $this->attributes['sites'] = $value;
    }
}

Then;

$my_model->sites = [1,2,3];
$my_model->sites; // [1,2,3]

You might want to set an initial default value for $this->attributes['sites'] in the model constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

Another option would be to use protected $attributes = ['sites' => []] instead of the constructor.
1

The problem lies with the $this->sites. It is correct that it will throw an exception that you class instance does not have a property $sites.

Take a look at the __get method of Model. You can see here that a getAttribute($key) call is being made. Diving even deeper you would find out that at some point the following code is being executed in the transformModelValue method:

if ($this->hasGetMutator($key)) {
    return $this->mutateAttribute($key, $value);
}

This piece of code calls your getSitesAttribute method.

Going back a little bit and you will see that it tries to retrieve the value for the $key (sites) from the attributes array by calling the getAttributeFromArray($key). This will return the value stored in the attributes array which is where your setSitesAttribute mutator stores the value.

The following options I could come up with:

  1. Remove the accessor to let the model retrieve the value from the $attributes array.
protected $appends = ['sites'];

public function setSitesAttribute($value)
{
    $this->attributes['sites'] = $value;
}
  1. Store value in and retrieve from $sites property. Not sure if $appends still works.
protected $appends = ['sites'];

private $sites = [];

public function getSitesAttribute()
{
    return $this->sites;
}

public function setSitesAttribute($value)
{
    $this->sites = $value;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.