因此,我正在为Laravel
中的类创建一个项目,其他用户将通知用户他们正在关注,但我发现很难查询数据。
这是我的代码:
use Appfollowus;
块引用
$follow=followus::select('followerid')->where('followingid',auth()->user()-
>id)->get();
$followers=User::where('id',$follow)->get();
$follow
作为集合返回,这会导致$followers
返回Null。
在此处输入图像描述
当我将first()
添加到$follow
时,例如:
$follow=followus::select('followerid')->where('followingid',auth()->user()->id)->first()->followerid;
它返回原始值,然后该值正在工作,但在这种情况下,我只能通知整个列表中的一个用户,这是我不想要的。
有人可以帮我解决这个问题,因为我对 mysql 不是很熟悉。
为了获取追随者值的集合,您需要使用pluck((方法而不是get((:
$followerIds=followus::where('followingid',auth()->user()->id)->pluck('followerid');
为了稍后按 ID 列表进行过滤,您需要使用whereIn(( 而不是where((:
$followers=User::whereIn('id',$followerIds)->get();