我有麻烦写在laravel雄辩的ORM查询。

我的问题是

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

现在我想把这个问题转换成laravel雄辩。


查询构建器:

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

口才:

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

你也可以用下面的方式使用WhereNotIn:

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

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


实现whereNotIn的动态方式:

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

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

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

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

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

$otherCompany = User::whereNotIn('id', $user)->get();

你可以照着做。

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

在我添加方法->toArray()到结果之前,我有一个子查询问题,我希望它能帮助更多,因为我有一个很好的时间寻找解决方案。

例子

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

它只是意味着你有一个值的数组,你想记录除了values/records。

你可以简单地传递一个数组到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.

这是Laravel 7的工作变体

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

$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();

或者试着在laravel里拔一下 在这里

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

查询构建器:

    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();