Laravel “拉雷维尔雄辩”;如果不在“中”;

Laravel “拉雷维尔雄辩”;如果不在“中”;,laravel,laravel-4,eloquent,Laravel,Laravel 4,Eloquent,我在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。查询生成器: DB::table(..)->select(..)->whereNotIn('book_price', [1

我在
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。

查询生成器:

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

SomeModel::select(..)->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();

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

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

这将返回带有特定字段的记录的集合,动态执行方式,其中不包括:

 $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`

您可以使用此示例动态调用不在中的

$user = User::where('company_id', '=', 1)->select('id)->get()->toArray(); $otherCompany = User::whereNotIn('id', $user)->get(); $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();
它只是意味着您有一个值数组,并且除了值/记录之外,您还需要记录

您只需将数组传递到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();

或尝试在laravel中拨弄 这里

查询生成器:

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

SomeModel::select(..)->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();

select
可以替换为
get
中的数组。因此比在官方文档中搜索更快!回答得好。这对我很有帮助。@Marwelln,是的,但它不会处理复杂的查询。例如,使用addSelect方法。您的示例过于复杂<代码>用户::orderBy('name','DESC')->where('status','!=',0)->get()感谢您的回答,但这或多或少是现有答案的重复。是的,但这就是在laravel中执行notin的方式。无论如何,谢谢你的评论。