如何修复whereNotExists和查询构建器Laravel

时间:2019-06-26 05:12:02

标签: php laravel

我尝试结合whereNotExists和在哪里,如果同时使用它,并且可以尝试其中之一,则可以

    $query = DB::table('tabel_produk')->where('kode_customer',$cust)->whereNotExists(function($query){
$query->select(DB::raw(1))->from('tabel_detail_realisasi')->whereRaw('tabel_detail_realisasi.barcode = tabel_produk.barcode');
})->distinct()->orderBy('barcode','asc') ->get();

请帮助我

1 个答案:

答案 0 :(得分:0)

我没有您的数据库架构,所以我尝试过使用本地数据库进行类似的操作,并且看起来工作正常。请参阅下面的示例。您能否将我的查询替换为您的查询,然后查看查询记录器输出什么?

    DB::flushQueryLog();
    DB::enableQueryLog();
    $results = DB::table('product_templates')
        //Non deleted products
        ->where('deleted', '=', '0')
        //
        ->whereNotExists(function ($query) {
            //Where the category is not 'General'
            $query
                ->select(DB::raw(1))
                ->from('product_categories')
                ->whereRaw('product_categories.id = product_templates.category_id')
                ->where('product_categories.name', 'General');
        })
        ->distinct()
        ->orderBy('name', 'asc')->get();
    DB::disableQueryLog();
    $queryLog = DB::getQueryLog();
    logger("Query results", compact('results', 'queryLog'));