gpt4 book ai didi

mysql - Eloquent 查询结果受关系标准限制

转载 作者:行者123 更新时间:2023-11-29 12:24:05 25 4
gpt4 key购买 nike

我有两个表,即附属机构和 session ,我想构建 Eloquent 查询,它将列出所有附属机构,但不包括在指定日期和时间指定 session 的附属机构。因此,结果应包含所有没有召开任何 session 的联属机构以及那些召开了 session 但未在指定日期和时间举行的联属机构。

用户:ID、姓名、城市...

session :id、客户、日期、时间、user_id...

是否可以在没有连接或原始 sql(whereRaw 或 DB::raw)的情况下完成以及如何完成?

测试数据用户:

array:3 [▼
0 => array:5 [▼
"id" => 2
"name" => "John"
"city" => "New York"
"created_at" => "2015-02-19 00:26:43"
"updated_at" => "2015-02-19 00:34:03"
]
1 => array:5 [▼
"id" => 3
"name" => "Elisabeth"
"city" => "Kansas City"
"created_at" => "2015-02-19 00:26:43"
"updated_at" => "2015-02-19 00:26:43"
]
2 => array:5 [▼
"id" => 4
"name" => "Teodora"
"city" => "New York"
"created_at" => "-0001-11-30 00:00:00"
"updated_at" => "-0001-11-30 00:00:00"
]
]

测试数据 session :

array:3 [▼
0 => array:8 [▼
"id" => 1
"client" => "George P."
"date" => "2015-05-15"
"time" => "14:00:00"
"approved" => 1
"user_id" => 2
"created_at" => "2015-02-19 00:26:43"
"updated_at" => "2015-02-19 00:26:43"
]
1 => array:8 [▼
"id" => 2
"client" => "Jack White"
"date" => "2015-05-15"
"time" => "12:00:00"
"approved" => 1
"user_id" => 2
"created_at" => "2015-02-19 00:26:43"
"updated_at" => "2015-02-19 00:26:43"
]
2 => array:8 [▼
"id" => 3
"client" => "Philip B."
"date" => "2015-05-16"
"time" => "16:00:00"
"approved" => 1
"user_id" => 3
"created_at" => "2015-02-19 00:26:43"
"updated_at" => "2015-02-19 00:26:43"
]
]

2015-05-15 12:00 没有开会的所有用户的预期结果

array:2 [▼
0 => array:5 [▼
"id" => 3
"name" => "Elisabeth"
"city" => "Kansas City"
"created_at" => "2015-02-19 00:26:43"
"updated_at" => "2015-02-19 00:26:43"
]
1 => array:5 [▼
"id" => 4
"name" => "Teodora"
"city" => "New York"
"created_at" => "-0001-11-30 00:00:00"
"updated_at" => "-0001-11-30 00:00:00"
]
]

感谢任何帮助。

最佳答案

假设您的用户模型具有关系 session ,您可以执行以下操作:

$users = User::whereDoesntHave('meetings', function($q){
$q->where('time', '12:00:00');
})->get();

关于mysql - Eloquent 查询结果受关系标准限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28605435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com