samedi 22 décembre 2018

Accessing values inside the model

I would like to access table data inside a relationship method within the model. But when i run an if statement to check if the type is 'item' then it returns: Call to a member function addEagerConstraints() on null

So it isn't recognizing $this->type. How would achieve this?

I have tried to use $this->type to see if I could check the type in this way but no luck.

class PurchasableItem extends Model
{
    protected $fillable = ['type', 'value', 'amount'];

    function item_data() {
        if($this->type == 'item')
            return $this->hasOne('App\ItemTemplate', 'id', 'value');
    }
}

So I want it to return this relationship only if the type of the purchasable item is of value 'item' but when i try to run this i get the following error: Call to a member function addEagerConstraints() on null



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire