【问题标题】:Eloquent where condition AND ( condition OR condition OR condition )雄辩 where 条件 AND(条件 OR 条件 OR 条件)
【发布时间】:2020-02-29 21:07:53
【问题描述】:

谁能告诉我我做错了什么? 我想获取所有状态为 0 的项目,并且如果有任何数据以 eloquent 匹配。 我的代码是这样的

public function search(Request $request){
        if($request->has('q')){
            
            $search_query = $request->input('q');

            $projects = Project::where(['status' => '0'])->get();

            $projects = $projects->where(function($query) use ($search_query) {
                $query->where('title','LIKE','%'.$search_query.'%')
                      ->orWhere('shortDescription','LIKE','%'.$search_query.'%')
                      ->orWhere('longDescription','LIKE','%'.$search_query.'%')
                      ->orWhere('tags','LIKE','%'.$search_query.'%')
                      ->orWhere('projectLink','LIKE','%'.$search_query.'%');
            });
            $projects->get();

            dd($projects);
         }
}

我收到了这个错误

错误异常
explode() 期望参数 2 是字符串,给定对象

【问题讨论】:

  • 我解决了 explode() 问题。我正在使用 get() 两次。现在我有另一个问题。查询每次都返回 false

标签: laravel eloquent


【解决方案1】:

我解决了这个问题,但稍微更改了代码......如果你喜欢,请接受!我测试过,它工作正常。

if($request->has('q')){

        // Your $request->q Value
        $term = $request->q;

        $projects = DB::table('tbl_projects')->where('status', '=', 0) 
                    ->where(function ($query) use ($term) {
                        $query->where('title', 'LIKE', '%'.$term.'%')
                        ->orWhere('shortDescription', 'LIKE', '%'.$term.'%')
                        ->orWhere('longDescription', 'LIKE', '%'.$term.'%')
                        ->orWhere('tags', 'LIKE', '%'.$term.'%')
                        ->orWhere('projectLink', 'LIKE', '%'.$term.'%');
                   })
                   ->get();

        //dd($projects);
    }

原始查询是:

select * from `tbl_projects` where `status` = 0 and (`title` LIKE %bla bla% or `shortDescription` LIKE %bla bla% or `longDescription` LIKE %bla bla% or `tags` LIKE %bla bla% or `projectLink` LIKE %bla bla%))

问候!

【讨论】:

    【解决方案2】:

    你的错误是

    $projects = Project::where(['status' => '0'])->get();
    
    1. (['status' => '0']) Where 子句所需的参数不同。您必须使用 ('status', '0')('status', '=', '0')
    2. 当你使用 ->get() 时,你有一个 Collection 或 EloquentCollection 对象。你需要一个 QueryBuilder

    尝试实现更好的编程逻辑。

    更好的实现是

    public function search(Request $request){
        if($request->has('q')){
             // Add array with field coincidences target. Is better if this array is a const static field in the Project model
            $searchFields = ['title', 'shortDescription', 'longDescription', 'tags', 'projectLink' ]; 
            // Now you can add 100 fields for search coincidences easily
    
            $search_query = $request->input('q');
    
            $projects = Project::where('status', '0');
            foreach($searchFields as $searchField){
                if ($searchField == 'title'){
                    $projects = $projects->where($searchField,'LIKE',"%$search_query%");
                    continue;
                }
                   $projects = $projects->orWhere($searchField,'LIKE',"%$search_query%");
            }
            $projects = $projects->get();
    
            dd($projects);
         }
    }
    

    【讨论】:

    • 这将要求每个searchField 匹配才能返回结果。将$projects->where() 更改为$projects->orWhere()
    • @Erich 你是对的。我还改进了答案以便更好地理解
    • 你在第一个版本中做得更好。请注意,您在初始 $projects = Project::where('status', '0'); 中已经有一个 where,因此 foreach 中的额外条件是不必要的。把它们都做成orWheres。 :)
    • 我想使用 orWhere 子句的原因是我不想循环。 @hmrneves 解决方案对我有用。非常感谢大家
    猜你喜欢
    • 1970-01-01
    • 2013-11-09
    • 2015-07-09
    • 1970-01-01
    • 2016-04-24
    • 2013-04-07
    • 2015-09-11
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多