我正在尝试使用Laravel在我的网站中实现高级搜索选项。搜索3到4表以获得所需的结果。我写了db查询,我得到了所需的结果作为输出,但我需要将其转换为eloquent,我已经写了一些,但没有像db查询那样获得所需的结果。知道如何实现它吗?
数据库查询
$product = DB::table('products')
->select(['products.id', 'products.title','package.name','products.product_summary','products.description'
,'package.description'
,'products.min_price','products.rating_count'])
->leftjoin('package',function($q){
$q->on('products.package_id' ,'package.id');
})
->leftjoin('package_packagecategories',function($q){
$q->on('package.id','package_packagecategories.package_id');
})
->leftjoin('product_categories',function($q){
$q->on('package_packagecategories.category_id','product_categories.id');
})
->leftjoin('country',function($q){
$q->on('products.country','country.id');
})
->where('products.status',1)
->Where('products.title','like',"%{$string}%")
->orWhere('products.product_summary', 'like', "%{$string}%")
->orWhere('products.description', 'like', "%{$string}%")
->orWhere('package.name', 'like', "%{$string}%")
->orWhere('package.description', 'like', "%{$string}%")
->orWhere('package.product_summary', 'like', "%{$string}%")
->orWhere('country.name', 'like', "%{$string}%")
->get();
以及为上述数据库查询到目前为止所写的雄辩
Product::select('id', 'title','product_summary','description','min_price','rating_count')
->WhereHas('Package',function($q)use($string){
$q->orWhere(DB::raw('LOWER(name->>"$.en")'),'like',"%{$string}%")
->orWhere(DB::raw('LOWER(description->>"$.en")'),'like',"%{$string}%")
->orWhere(DB::raw('LOWER(product_summary->>"$.en")'),'like',"%{$string}%")
->orWhereHas('categories',function($subq)use($string){
$subq->orWhere(DB::raw('LOWER(name->>"$.en")'),'like',"%{$string}%");
});
})
->WhereHas('country',function($q)use($string){
$q->orWhere('name','like',"%{$string}%");
})
我需要像db查询那样得到第二个代码的结果。
最后能够找出一个有效的代码..
$product->where(function($p)use($string){$p->orWhereHas('country',function($q)use($string){
$q->where('name','like',"%".strtolower($string)."%");
})
->orWhere( DB::raw('LOWER(products.title)'),'like',"%".strtolower($string)."%")
->orWhere(DB::raw('LOWER(products.product_summary)'),'like',"%".strtolower($string)."%")
->orWhere(DB::raw('LOWER(products.description)'),'like',"%".strtolower($string)."%")
->orWhereHas('categories',function($subq)use($string){
$subq->where(DB::raw('LOWER(name)'),'like',"%".strtolower($string)."%");
})
->orWhereHas('Package',function($subq)use($string){
$subq->where(DB::raw('LOWER(name)'),'like',"%".strtolower($string)."%");
})
->orWhereHas('Package',function($subq)use($string){
$subq->where(DB::raw('LOWER(description)'),'like',"%".strtolower($string)."%");
})
->orWhereHas('Package',function($subq)use($string){
$subq->where(DB::raw('LOWER(product_summary)'),'like',"%".strtolower($string)."%");
})
->orWhereHas('Package',function($subq)use($string){
$subq->whereHas('categories',function($subq)use($string){
$subq->where(DB::raw('LOWER(name)'),'like',"%".strtolower($string)."%");
});
})
->orWhereHas('Package',function($subq)use($string){
$subq->whereHas('country',function($q)use($string){
$q->where('country.name','like',"%".strtolower($string)."%");
});
});
});