Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何界定「;跟随;laravel 5.2中的关系_Php - Fatal编程技术网

Php 如何界定「;跟随;laravel 5.2中的关系

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

最近,我使用Laravel来定义用户之间的“跟随”关系。以下是模型:

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');
    }
}
因为您提供的型号名称不正确