[英]Complex Query in Laravel Query Builder
I've made a Search function within my site.我在我的网站中做了一个搜索功能。 However my old query was bringing in deleted_at columns, Despite them being null.
然而,我的旧查询引入了 Deleted_at 列,尽管它们为空。
So I've written the query in raw MySQL and am getting the results I expect back.所以我用原始 MySQL 编写了查询,并得到了我期望的结果。
However, I am struggling to actually write this using Laravel's Query Builder.但是,我正在努力使用 Laravel 的查询生成器来实际编写它。 The working MySQL Query I'd like is :
我想要的工作 MySQL 查询是:
select * from `packs`
left join `keywords`
on `keywords`.`pack_id` = `packs`.`pack_id`
inner join `categories`
on `categories`.`category_id` = `packs`.`primary_category_id`
left join `ratings`
on `ratings`.`pack_id` = `packs`.`pack_id`
where (`pack_title` LIKE '%sams%'
or `keywords`.`keyword_title` LIKE '%sams%')
and `packs`.deleted_at is null
group by `pack_title`
order by `packs`.`created_at`
desc
My current attempt using Laravel looks as so :我目前使用 Laravel 的尝试如下所示:
// Explode Terms
$terms = explode(' ', $q);
// Produce Query (Initially)
$query = DB::table('packs')
->leftJoin('keywords', 'keywords.pack_id', '=', 'packs.pack_id')
->leftJoin('categories', 'categories.category_id', '=', 'packs.primary_category_id')
->whereNotNull('packs.deleted_at')
->leftJoin('ratings', 'ratings.pack_id', '=', 'packs.pack_id');
// Loop through each term
foreach($terms as $term)
{
$query->where('pack_title', 'LIKE', '%'. $term . '%')
->orWhere(function($query, $term)
{
$query->orWhere('pack_description', 'LIKE', '%'. $term . '%')
->orWhere('keywords.keyword_title', 'LIKE', '%'. $term . '%');
})
->whereNotNull('packs.deleted_at')
->groupBy('pack_title')
->orderBy('packs.created_at', 'DESC');
}
// Log
Log::info('User Searched using term : '.$q.'');
$results = $query->get();
This is producing the error :这是产生错误:
Missing argument 2 for SearchesController::{closure}()
SearchesController::{closure}() 缺少参数 2
Is this possible to write in Query Builder, If so how.这是否可以在查询生成器中编写,如果可以,如何编写。 I don't mind exploring writing it as a RAW Query if needs be.
如果需要,我不介意探索将其编写为 RAW 查询。
Thanks谢谢
Try this:尝试这个:
$terms = explode(' ', $q);
// Produce Query (Initially)
$query = DB::table('packs')
->leftJoin('keywords', 'keywords.pack_id', '=', 'packs.pack_id')
->leftJoin('categories', 'categories.category_id', '=', 'packs.primary_category_id')
->whereNotNull('packs.deleted_at')
->leftJoin('ratings', 'ratings.pack_id', '=', 'packs.pack_id');
// Loop through each term
foreach($terms as $term)
{
$query->where('pack_title', 'LIKE', '%'. $term . '%')
->orWhere(function($query) use ($term)
{
$query->orWhere('pack_description', 'LIKE', '%'. $term . '%')
->orWhere('keywords.keyword_title', 'LIKE', '%'. $term . '%');
})
->whereNotNull('packs.deleted_at')
->groupBy('pack_title')
->orderBy('packs.created_at', 'DESC');
}
// Log
Log::info('User Searched using term : '.$q.'');
$results = $query->get();
Note: I made the change over here ->orWhere(function($query) use ($term)
注意:我在这里做了更改
->orWhere(function($query) use ($term)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.