A
A
Alex Wells2017-08-07 15:46:55
ORM
Alex Wells, 2017-08-07 15:46:55

Eloquent model where relation morphs?

Hello. There is a model, Item, used throughout the project. In order not to fence dozens of pivot tables, I decided to make it simpler:

class Item extends BaseModel
{
    public function owner()
    {
        return $this->morphTo();
    }
}

Now, in the service that manages this whole thing, I want to pull in all the Item models whose owner is equal to the instance of the model, for example - User. I am looking for a method of the type Pull on not to offer, because then I will have to enter relationships in each model (or am I wrong?)
Item::whereRelation('owner', $user)->get()
$user->items

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2017-08-07
@Alex_Wells

I realized that in other parts of the service you will also have to use $model->items(), so it's closed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question