Php 如何界定「;跟随;laravel 5.2中的关系
最近,我使用Laravel来定义用户之间的“跟随”关系。以下是模型:Php 如何界定「;跟随;laravel 5.2中的关系,php,Php,最近,我使用Laravel来定义用户之间的“跟随”关系。以下是模型: class User extends Model { public function follows() { return $this->morphMany('App\User', 'followable'); } } class Follow extends Model { public function followable() { retur
class User extends Model
{
public function follows()
{
return $this->morphMany('App\User', 'followable');
}
}
class Follow extends Model
{
public function followable()
{
return $this->morphTo();
}
}
下面的数据库如下所示:
follows:
- id:
- user_id:
- followable_id:
- followable_type:
$follow = Follow:find(1);
$user = $follow->followable;
$followers = $user->follows;
所有这些都是根据提供的示例定义的
现在我可以像这样检索用户模型:
follows:
- id:
- user_id:
- followable_id:
- followable_type:
$follow = Follow:find(1);
$user = $follow->followable;
$followers = $user->follows;
但当我编写这样的代码时:
follows:
- id:
- user_id:
- followable_id:
- followable_type:
$follow = Follow:find(1);
$user = $follow->followable;
$followers = $user->follows;
我发现错误:
Relationship method must return an object of type
Illuminate\Database\Eloquent\Relations\Relation
我的问题是:
我是否定义了“跟随”的关系?我如何修复错误
谢谢。试试这个
class User extends Model
{
public function follows()
{
return $this->morphMany('App\User', 'followable');
}
}
换成
class User extends Model
{
public function follows()
{
return $this->morphMany('App\Follow', 'followable');
}
}
因为您提供的型号名称不正确。试试这个
class User extends Model
{
public function follows()
{
return $this->morphMany('App\User', 'followable');
}
}
换成
class User extends Model
{
public function follows()
{
return $this->morphMany('App\Follow', 'followable');
}
}
因为您提供的型号名称不正确