如何获取不在laravel表下的belongsToMany关系下的用户列表? [英] how to get list of users who are not under belongsToMany relation under a table in laravel?
问题描述
请考虑以下情况.
我们有Users
表和Tasks
表.它们与表task_user
中的belongsToMany
有关.
Consider the following case.
we have the Users
table and Tasks
table. they are in relation with belongsToMany
with table task_user
.
如何获取不在任何任务下的所有用户的列表?即他们的user_id根本不在该给定任务下,甚至不在task_user
表中.
How to get the list of all users who are not under any task? i.e. their user_id is not at all under that given task or even in the task_user
table.
我之所以需要这样做是因为这样,我们只能提供尚未分配任务的用户列表.该任务将一次分配给用户,而不是一次分配给单个用户.
why I need this is because like this we can only provide a list of users who are yet to be assigned a task. the task will be assigned to users and not a single user at a time.
编辑_____________
Editing_____________
还如何根据组表与用户进行过滤?下面不起作用
also how to filter with users based on group table? below is not working
$users = Group::with(['subscribers' => function ($q){
$q->doesntHave("tasks");
}])->whereId($gid)->latest()->get();
推荐答案
假设您已正确命名关系,则应该可以使用doesntHave("tasks")
:
Assuming you've named your relationships properly, you should be able to use doesntHave("tasks")
:
$tasklessUsers = User::doesntHave("tasks")->get();
doesntHave()
检查是否存在所提供的关系(在本例中为"tasks"
),并返回所有通过此检查的对象.
doesntHave()
checks for the non-existence of the supplied relationship ("tasks"
, in this case) and returns all objects that pass this check.
如果您的函数名称不同,请使用该名称,但关系应为:
If your function name is different, use that, but the relationship should be:
User.php
:
public function tasks(){
return $this->belongsToMany(Task::class, "task_user");
}
doesntHave()
是简单版本,whereDoesntHave()
允许自定义查询.参见 https://laravel.com/docs/5.8/eloquent-relationships #querying-relationship-absence 获取详细信息.
doesntHave()
is the simple version, whereDoesntHave()
allows a custom query. See https://laravel.com/docs/5.8/eloquent-relationships#querying-relationship-absence for full details.
第二次修改:
如下面的评论所述,with()
不会过滤正在被调用的Model
,因此此查询无法按您期望的那样工作:
As stated in the comments below, with()
will not filter the Model
it is being called on, so this query won't work as you'd expect:
$users = Group::with(['subscribers' => function ($q){
$q->doesntHave("tasks");
}])->whereId($gid)->latest()->get();
要解决此问题,请使用链接的doesntHave()
查询:
To fix this, use a chained doesntHave()
query:
$query = Group::doesntHave('subscribers.tasks')
->where('id', '=', $gid)
->latest()
->first();
// OR
$query = Group::whereHas('subscribers', function($subQuery){
$subQuery->doesntHave('tasks');
})->where('id', '=', $gid)
->latest()
->first();
$users = $query->subscribers; // Return `users` (aliased to `subscribers`)
这两种方法都会检查是否存在subscribers
没有任何关联的tasks
关系,并且只会在id
是$gid
的情况下返回.
Either approach will check the existence of subscribers
that don't have any associated tasks
relationship, and also only return where id
is $gid
.
注意:将first()
用于查询,因为在查询中使用id
只能返回单个Group
记录,而get()
用于返回Collection
Note: Used first()
for the queries, as using id
in a query should only ever return a single Group
record, and get()
is for returning multiple records in a Collection
这篇关于如何获取不在laravel表下的belongsToMany关系下的用户列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!