Answer the question
In order to leave comments, you need to log in
How to get data through a table?
There is a table
A user can follow a category or another user.
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');
}
}
Answer the question
In order to leave comments, you need to log in
$user = User::find(1);
$folowings = $user->followings;
foreach ($folowings as $folowing) {
dump($folowing->followable);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question