-
Relation 表结构
- id
- userId
- toUserID
- relation
-
要求:筛选出没有与本用户建立关系的用户
-
高效写法
$users = User::whereNotIn('id', function ($query) use($user){
$query->select('toUserId')->from('relation')->where('relation.userId', $user->id);
});
- 低效率写法
$actionRelationsArray = Relation::select('toUserId')->where('userId', $user->id)->get()->pluck('toUserId');
$users = User::whereNotIn('id', $actionRelationsArray);
网友评论