如何以原始格式而不是集合格式从数据库表中返回值?



因此,我正在为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();

最新更新