我在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();
雄辩:
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();
whereNotIn方法验证给定列的值是否包含在给定数组中:
$users = DB::table('users')
->whereNotIn('id', [1, 2, 3])
->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();