【问题标题】:Laravel Eloquent "WHERE NOT IN"Laravel 雄辩的“WHERE NOT IN”
【发布时间】:2014-11-09 00:53:04
【问题描述】:

我在laravel eloquent ORM 中编写查询时遇到问题。

我的查询是

SELECT book_name,dt_of_pub,pub_lang,no_page,book_price  
FROM book_mast        
WHERE book_price NOT IN (100,200);

现在我想把这个查询转换成 laravel eloquent。

【问题讨论】:

    标签: laravel laravel-4 eloquent


    【解决方案1】:

    查询生成器:

    DB::table(..)->select(..)->whereNotIn('book_price', [100,200])->get();
    

    雄辩:

    SomeModel::select(..)->whereNotIn('book_price', [100,200])->get();
    

    【讨论】:

    • select 可以替换为get 中的数组。
    • SO比在官方文档中搜索要快!
    • 很好的回答。这对我很有帮助。
    • @Marwelln,是的,但它不适用于复杂的查询。例如。使用 addSelect 方法。
    【解决方案2】:

    您也可以通过以下方式使用 WhereNotIn

    ModelName::whereNotIn('book_price', [100,200])->get(['field_name1','field_name2']);
    

    这将返回具有特定字段的记录集合

    【讨论】:

      【解决方案3】:

      在我将方法 ->toArray() 添加到结果之前,我在进行子查询时遇到了问题,我希望它对一个以上的帮助有所帮助,因为我在寻找解决方案的过程中度过了愉快的时光。

      示例

      DB::table('user')                 
        ->select('id','name')
        ->whereNotIn('id', DB::table('curses')->select('id_user')->where('id_user', '=', $id)->get()->toArray())
        ->get();
      

      【讨论】:

      • ->pluck('id_user') 而不是 ->toArray() 对我有用
      【解决方案4】:

      查询生成器:

          DB::table('book_mast')
      ->select('book_name','dt_of_pub','pub_lang','no_page','book_price')
          ->whereNotIn('book_price', [100,200])->get();
      

      雄辩:

      BookMast::select('book_name','dt_of_pub','pub_lang','no_page','book_price')
      ->whereNotIn('book_price', [100,200])->get();
      

      【讨论】:

      • 感谢您的回答,但它或多或少与现有答案重复。
      • 是的,但这就是在 laravel 中 whereNotIn 的完成方式。无论如何感谢您的评论。
      【解决方案5】:

      whereNotIn的动态实现方式:

       $users = User::where('status',0)->get();
          foreach ($users as $user) {
                      $data[] = $user->id;
                  }
          $available = User::orderBy('name', 'DEC')->whereNotIn('id', $data)->get();
      

      【讨论】:

      • 您的示例过于复杂。 User::orderBy('name', 'DESC')->where('status', '!=',0)->get()
      【解决方案6】:

      您可以使用此示例动态调用 Where NOT IN

      $user = User::where('company_id', '=', 1)->select('id)->get()->toArray();
      
      $otherCompany = User::whereNotIn('id', $user)->get();

      【讨论】:

        【解决方案7】:

        您可以通过以下方式使用WhereNotIn

        $category=DB::table('category')
                  ->whereNotIn('category_id',[14 ,15])
                  ->get();`enter code here`
        

        【讨论】:

          【解决方案8】:

          您可以执行以下操作。

          DB::table('book_mast') 
          ->selectRaw('book_name,dt_of_pub,pub_lang,no_page,book_price')  
          ->whereNotIn('book_price',[100,200]);
          

          【讨论】:

            【解决方案9】:

            它只是意味着你有一个值数组,并且你想要记录,除了值/记录。

            您可以简单地将数组传递给 whereNotIn() laravel 函数。

            使用查询生成器

            $users = DB::table('applications')
                                ->whereNotIn('id', [1,3,5]) 
                                ->get(); //will return without applications which contain this id's
            

            口若悬河。

            $result = ModelClassName::select('your_column_name')->whereNotIn('your_column_name', ['satatus1', 'satatus2']); //return without application which contain this status.
            

            【讨论】:

              【解决方案10】:

              这是我为 Laravel 7 工作的变体

              DB::table('user')                 
                ->select('id','name')
                ->whereNotIn('id', DB::table('curses')->where('id_user', $id)->pluck('id_user')->toArray())
                ->get();
              

              【讨论】:

                【解决方案11】:

                或尝试在 laravel 中采摘 这里

                DB::table('user')                 
                          ->select('id','name')
                          ->whereNotIn('id', DB::table('curses')->where('id_user', '=', $id)->pluck('user_id'))
                          ->get();  
                

                【讨论】:

                  【解决方案12】:
                  $created_po = array();
                       $challan = modelname::where('fieldname','!=', 0)->get();
                       // dd($challan);
                       foreach ($challan as $rec){
                           $created_po[] = array_push($created_po,$rec->fieldname);
                       }
                       $data = modelname::whereNotIn('fieldname',$created_po)->orderBy('fieldname','desc')->with('modelfunction')->get();
                  

                  【讨论】:

                    猜你喜欢
                    • 2020-09-28
                    • 1970-01-01
                    • 1970-01-01
                    • 2017-11-05
                    • 1970-01-01
                    • 2015-08-08
                    • 1970-01-01
                    • 2021-09-29
                    • 2016-07-05
                    相关资源
                    最近更新 更多