Laravel Eloquent“在哪里”

问题描述 投票:114回答:8

我在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
8个回答
238
投票

查询生成器:

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

雄辩:

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

19
投票

您也可以按以下方式使用WhereNotIn:

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

这将返回包含特定字段的Record集合


2
投票

实现whereNotIn的动态方式:

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

1
投票

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

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

1
投票

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

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

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

0
投票

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

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

0
投票

你可以做以下。

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

0
投票

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

DB::table('user')                 
  ->select('id','name')
  ->whereNotIn('id', DB::table('curses')->select('id_user')->where('id_user', '=', $id)->get()->toArray())
  ->get();
© www.soinside.com 2019 - 2024. All rights reserved.