Polymorphic relation in laravel for following a user -
i've user
model , likes
table. applied polymorphic relation it.
likes
table has following structure:
user_id --- likeable_id ---- likeable_type eg: 1 ---- 5 ---- app\user //this indicates user 1 follows 5 eg: 3 ---- 1 ---- app\user //this indicates user 3 follows 1
now i'm trying followers , following list.
in user
model added
public function likes() { return $this->morphmany(like::class, 'likeable'); } public function followers() { return $this->belongsto('app\like', 'likeable_id', 'user_id'); }
and controller, i've tried this:
$user1 = user::with('followers')->where('username', $username)->first();
but it's returning null
followers
relation. doing right?
got answer myself: in user
model -
public function followers() { return $this->morphmany('app\like', 'likeable')->with('user'); } public function following() { return $this->morphmany('app\like', 'user', 'likeable_type')->with('user'); }
Comments
Post a Comment