I am trying to implement an advanced searching option in my website using Laravel. Searching through 3 to 4 table to get the desired result. I wrote db query and I'm getting desired result as output but I need to convert this to eloquent, and I've written some of it, but not getting the desired result as db query does. Any idea how to achieve it?
DB query
$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();
and the eloquent written so far for the above db query
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}%");
})
I need to get the result for second code as db query do.
Finally abled to figured out a working code ..
$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)."%");
});
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.