获取以下页面

时间:2018-01-12 08:19:40

标签: laravel eloquent

如果用户关注或没有页面,我试图获取。我尝试这样:

    class Page extends Model
    {

        public function get_following()
        {
            return $this->belongsTo(Followers::class, 'user_id')->where('type', '=', 1)->where('follower_id', auth()->user()->id)->Select('id');
        }
}

但是这返回null。

DD($页)

    Page {#303 ▼
#attributes: array:9 [▼
    "id" => 1
]
      #relations: array:3 [▼
        "ptag" => Tag {#307 ▶}
        "get_following" => null

表追随者:

followers.user_id = (pages.id), follower_id = (auth user), type = (1)

谢谢。

3 个答案:

答案 0 :(得分:0)

将选择更改为->value('id')

public function get_following()
 {
   return $this->belongsTo(Followers::class, 'user_id')->where('type', '=', 1)->where('follower_id', auth()->user()->id)->value('id');
 }

答案 1 :(得分:0)

尝试:

public function get_following()
{
    return $this->belongsTo(Followers::class, 'user_id')
                ->where('type', '=', 1)
                ->where('follower_id', auth()->id())
                ->pluck('id');   
}

答案 2 :(得分:0)

public function get_following()
{
    return $this->belongsTo('\App\Followers', 'user_id')
                ->where('type', 1)
                ->where('follower_id', Auth::user()->id())
                ->pluck('id');   
}