A
A
adam_carraway2021-02-12 22:45:28
Laravel
adam_carraway, 2021-02-12 22:45:28

How to get data through a table?

There is a table 6026d83d5ed10747284568.png
A user can follow a category or another user.
6026d90b95e1f228611041.png
Standard links:

class Follower extends Model
{
    public function followable()
    {
        return $this->morphTo();
    }
}

class User extends Model
{
    public function followings()
    {
        return $this->morphMany(Follower::class, 'follower');
    }
}

The question is how can I immediately get the final tables through the links (does laravel provide such an opportunity?)
That is, if I display $user-> followings, then the models of users and categories that he followed will be returned to me, and not the table entries that are on the second screen

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Just Me, 2021-02-13
@adam_carraway

$user = User::find(1);
$folowings = $user->followings;
foreach ($folowings as $folowing) {
     dump($folowing->followable);
}

A
alex--n, 2021-02-12
@alex--n

If I understand correctly, then
with('relation_name')
Should work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question