判断关注人列表,为什么不是 return $this->followings ()->contains ($user_id)?
<?php
namespace App\Models;
.
.
.
class User extends Authenticatable
{
.
.
.
public function isFollowing($user_id)
{
return $this->followings->contains($user_id);
}
}
为什么是 return $this->followings->contains($user_id);
而不是return $this->followings()->contains($user_id);
因为
contains
方法是Collection
类的一个方法,$this->followings
返回的是一个Collection
类的实例,也就是一个集合,但是$this->followings()
返回的是一个Relations
,没有contains
方法,所以不能加括号。