php - How to define the "follow" relationship in laravel 5.2 -
recently use laravel define 'follow' relationship between users. here models:
class user extends model { public function follows() { return $this->morphmany('app\user', 'followable'); } } class follow extends model { public function followable() { return $this->morphto(); } }
and database follow this:
follows: - id: - user_id: - followable_id: - followable_type:
all above defined according examples laravel documents provides.
and can retrieve user model this:
$follow = follow:find(1); $user = $follow->followable;
but when write code this:
$followers = $user->follows;
i errors:
relationship method must return object of type illuminate\database\eloquent\relations\relation
here question: did define relationship of 'follow' right? , how can fix errors?
thanks.
try this
class user extends model { public function follows() { return $this->morphmany('app\user', 'followable'); } }
change in
class user extends model { public function follows() { return $this->morphmany('app\follow', 'followable'); } }
because have given model name incorrect.
Comments
Post a Comment