我有两个表,附属机构和会议,我想建立一个Eloquent查询,它将列出所有附属公司,而不是那些在指定日期和时间任命会议的人。因此,结果应该包含所有没有会议的会员以及那些有会议但没有指定日期和时间的会员。
用户:id,name,city ...
会议: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"
]
]
测试数据会议:
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"
]
]
任何帮助表示感谢。
答案 0 :(得分:1)
假设您的用户模型具有meetings
关系,您可以执行此操作:
$users = User::whereDoesntHave('meetings', function($q){
$q->where('time', '12:00:00');
})->get();