使用laravel eloquent搜索多个表

时间:2017-12-08 09:57:11

标签: php mysql laravel eloquent

我正在尝试使用Laravel在我的网站中实现高级搜索选项。搜索3到4表以获得所需的结果。 我写了db查询,我得到了所需的结果作为输出,但我需要将其转换为eloquent,我已经写了一些,但没有像db查询那样获得所需的结果。知道如何实现吗?

数据库查询

Is this documented anywhere that you know of, or is it just known through 
trial-and-error? – underscore_d 17 hours ago

Viewing query results in SSMS is a bit inconsistent. I wouldn't use SSMS 
results window for anything mission critical. – digital.aaron 17 hours ago

@underscore_d, not anywhere that I know of. It was, however, brought up in a 
side conversation in a Kimberley Tripp SQL Server training course I attended 
many years ago. – digital.aaron 17 hours ago

以及针对上述数据库查询到目前为止的雄辩说明

$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();

我需要获取第二个代码的结果作为db query do。

1 个答案:

答案 0 :(得分:0)

终于能够找到一个有效的代码..

$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)."%");
                                });
                            });
                        });