【问题标题】:How to find absence of relation in DB statement with join in Laravel如何在 Laravel 中使用 join 查找 DB 语句中的关系缺失
【发布时间】:2021-06-15 22:23:26
【问题描述】:

我正在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() 的帮助下生成查询生成器,我得到以下输出:

从projects 中选择p.id、p.slug、p.name、cp.updated_at 作为p 内部连接construction_product_projects 作为cpp 在p.id = cpp.@ 987654338@ 内连接construction_product 作为cp 在cpp.construction_product_id = cp.id 内连接project_associate_brand 作为pab 在cp.@9876@ 上987654350@ 和 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 = @ 987654366@.project_id 和 project_associate_brand.brand_id 不在 (?) 和 brands.deleted_at 为空)和 projects.deleted_at 为空

查询应该有where not exists,我的代码中没有。

【问题讨论】:

  • 您可以使用带条件second_tabled.id is NULL的左连接来检查是否存在关系
  • 我尝试使用空条件左连接 project_associate_brand 表,但没有工作。
  • 问题不在于 laravel 方法,而可能是您创建查询的方式。尝试从中生成原始查询并粘贴您的期望,这将有助于其他人更容易理解问题。并且可以帮助你
  • @RameshDahiya 我已经更新了问题,请查看

标签: laravel join eloquent laravel-query-builder


【解决方案1】:

您需要在此处使用 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',[?])
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 2019-12-19
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多