Laravel Eloquent "WHERE NOT IN"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25849015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:44:10  来源:igfitidea点击:

Laravel Eloquent "WHERE NOT IN"

laravellaravel-4eloquent

提问by Nur Uddin

I'm having trouble to write query in laravel eloquent ORM.

我在laravel eloquent ORM.

my query is

我的查询是

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

Now I want to convert this query into laravel eloquent.

现在我想将此查询转换为 Laravel eloquent。

回答by Jarek Tkaczyk

Query Builder:

查询生成器:

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

Eloquent:

雄辩:

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

回答by srmilon

You can use WhereNotInin following way also:

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

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

This will return collection of Recordwith specific fields

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

回答by Hari Pudyal

The dynamic way of implement whereNotIn:

whereNotIn 的动态实现方式:

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

回答by Vladimir Salguero

I had problems making a sub query until I added the method ->toArray()to the result, I hope it helps more than one since I had a good time looking for the solution.

在我将方法添加->toArray()到结果之前,我在进行子查询时遇到了问题,我希望它可以帮助不止一个,因为我很高兴寻找解决方案。

Example

例子

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

回答by ???? ????? ?????? ???? ? ?????

The whereNotIn method verifies that the given column's value is not contained in the given array:

whereNotIn 方法验证给定列的值不包含在给定数组中:

$users = DB::table('users')
                    ->whereNotIn('id', [1, 2, 3])
                    ->get();

回答by Zahid Hasan

You can use WhereNotInin the following way:

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

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

回答by Baiquni

You can use this example for dynamically calling the Where NOT IN

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

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

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

回答by khandar shailesh

You can do following.

您可以执行以下操作。

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