laravel - 如何在 Laravel 中使用 join 查找 DB 语句中的关系缺失
问题描述
我正在构建一个应用程序,我试图获取在 join语句Laravel 8.0
中不存在的关系。DB
我没有得到想要的结果
我尝试使用
public function ProductList() {
$innerQuery = DB::table('projects as p')
->join('construction_product_projects as cpp', 'p.id', '=', 'cpp.project_id')
->join('construction_product as cp', 'cpp.construction_product_id', '=', 'cp.id')
->where('cp.status', 'saved')
->join('project_associate_brand as pab', function ($join){
$join->on('cp.id', '=', 'pab.construction_product_id')
->when(request('group'), function ($q) {
$q->whereIn('pab.product_group_id', collect(request('group'))->pluck('id'));
})
->when(request('brand'), function ($q) {
$q->whereIn('pab.brand_id', collect(request('brand'))->pluck('id'))
->when(request('equivalent'), function ($q){
$q->where('pab.equivalent',true); //where project_associate_brand.equivalent is true
});
})
->when(request('brand_doesnt_have'), function ($q) {
// This is not working...
$q->whereNotIn('pab.brand_id', collect(request('brand_doesnt_have'))->pluck('id'));
});
})
->leftjoin('Construction_product_attachments as cpa', 'cpa.construction_product_id', '=', 'cp.id')
->select(DB::raw("p.id, p.slug, p.name, cp.updated_at"))
->whereNull('p.deleted_at')
->whereNull('cp.deleted_at')
->whereNull('cpa.deleted_at')
->groupByRaw('p.id');
return ProductListResource::collection(
DB::query()->fromSub($innerQuery, 't')
->select(DB::raw("id, slug, name, updated_at, attachments"))
->groupBy('t.id')
->paginate(50)
);
}
所以这不起作用所以我尝试使用whereExists
和whereNotIn
条件:
$q->whereExists(function ($query) {
$query->select(DB::raw(1))
->from('project_associate_brand')
->whereNotIn('project_associate_brand.brand_id', collect(request('brand_doesnt_have'))->pluck('id'));
});
欢迎任何更好的方法。谢谢。
PS:我也尝试使用该功能whereNotExists
但不起作用。我不能使用Eloquent
,whereDoesntHave
因为我在其中执行了很多复杂的查询/条件。
编辑:
toSql()
我尝试在得到以下输出的帮助下生成查询生成器:
select p.id, p.slug, p.name, cp.updated_at from projects
as p
inner join construction_product_projects
as cpp
on p
。id
= cpp
。project_id
内部连接construction_product
为cp
on cpp
。construction_product_id
= cp
。id
内部连接project_associate_brand
为pab
on cp
。id
= pab
。construction_product_id
和pab
。brand_id
不在 (?) 哪里p
。deleted_at
为空且cp
. deleted_at
按 p.id 为空组
但这不是预期的。我的期望如下:
select * from projects
where not exists (select * from brands
inner join project_associate_brand
on brands
. id
= project_associate_brand
. brand_id
where projects
. id
= project_associate_brand
. project_id
and project_associate_brand
. brand_id
not in (?) and brands
. deleted_at
is null) and projects
. deleted_at
一片空白
查询应该具有where not exists
我的代码中不存在的内容。
解决方案
您需要在此处使用 whereDoesntHave 以在查询中具有等效的 where not exists 子句。您期望的简单表示是:
Projects::whereDoesntHave('project_associate_brand', function($query) {
$query->join('brands', function($join) {
$join->on('brands.id','=','project_associate_brand.brand_id')
->whereNull('brands.deleted_at');
});
$query->whereNotIn('project_associate_brand.brand_id',[?])
})
推荐阅读
- mysql - AWS AURORA DB(My SQL 5.6) ver 1.22.2 Eclipse 链接 jpa 表数据消失(JAVA MAVEN)
- python - 贝尔曼福特(最短路径)python错误
- ios - 升级到 iOS 15 会导致生产应用程序图标发生变化
- package - 错误 0x01:签署 .msix 包时找不到指定的时间戳
- c# - 在 webassembly 中执行流式 GRPC 调用时不返回
- git - 根据 git describe 输出重建 make 目标
- python - 读取文件名,然后使用 python 调用可调用函数(带有一个或两个参数)
- arrays - 0和1的Concentrix矩阵
- css - 如何从带有样式组件的反应路由器的链接中删除样式
- c# - 基础连接已关闭。从 Windows Server 2012 中的任务计划程序进行计划时