doutan1875 2015-11-26 08:50
浏览 19
已采纳

Laravel查询生成器中的复杂查询

I've made a Search function within my site. However my old query was bringing in deleted_at columns, Despite them being null.

So I've written the query in raw MySQL and am getting the results I expect back.

However, I am struggling to actually write this using Laravel's Query Builder. The working MySQL Query I'd like is :

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 :

// 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}()

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.

Thanks

  • 写回答

1条回答 默认 最新

  • duansai1314 2015-11-26 09:04
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿