213

I'm having trouble to write query in 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.

Nur Uddin
  • 2,778
  • 6
  • 16
  • 22

13 Answers13

457

Query Builder:

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

Eloquent:

SomeModel::select(..)->whereNotIn('book_price', [100,200])->get();
Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157
39

You can use WhereNotIn in following way also:

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

This will return collection of Record with specific fields

18

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.

Example

DB::table('user')                 
  ->select('id','name')
  ->whereNotIn('id', DB::table('curses')->select('id_user')->where('id_user', '=', $id)->get()->toArray())
  ->get();
Vladimir Salguero
  • 5,609
  • 3
  • 42
  • 47
  • 2
    ->pluck('id_user') instead of ->toArray() works for me – CESILICON Aug 25 '21 at 14:05
  • This approach is not optimized, a sub query is better. ```DB::table('curses')->select('id_user')->where('id_user', '=', $id)->get()->toArray() ``` is performing a full DB query and retrieving results. There's 2 SQL query calls to the database in this case. Suggest to use ```->whereNotIn('id', function($q){ $q->table('curses')->select('id_user')->where('id_user', '=', $id); })``` instead. – Bill L. May 27 '22 at 03:54
12

Query Builder:

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

Eloquent:

BookMast::select('book_name','dt_of_pub','pub_lang','no_page','book_price')
->whereNotIn('book_price', [100,200])->get();
samzna
  • 405
  • 4
  • 8
5

The dynamic way of implement whereNotIn:

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

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

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

$otherCompany = User::whereNotIn('id', $user)->get();
Baiquni
  • 51
  • 3
2

You can use WhereNotIn in the following way:

$category=DB::table('category')
          ->whereNotIn('category_id',[14 ,15])
          ->get();`enter code here`
zx485
  • 28,498
  • 28
  • 50
  • 59
Zahid Hasan
  • 365
  • 3
  • 5
1

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]);
Rahul
  • 18,271
  • 7
  • 41
  • 60
1

This is my working variant for Laravel 7

DB::table('user')                 
  ->select('id','name')
  ->whereNotIn('id', DB::table('curses')->where('id_user', $id)->pluck('id_user')->toArray())
  ->get();
Stefan Pavlov
  • 398
  • 2
  • 8
0

Its simply means that you have an array of values and you want record except that values/records.

you can simply pass a array into whereNotIn() laravel function.

With query builder

$users = DB::table('applications')
                    ->whereNotIn('id', [1,3,5]) 
                    ->get(); //will return without applications which contain this id's

With eloquent.

$result = ModelClassName::select('your_column_name')->whereNotIn('your_column_name', ['satatus1', 'satatus2']); //return without application which contain this status.
Vinay Kaithwas
  • 1,415
  • 10
  • 17
0
$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();
0

or try pluck in laravel here

DB::table('user')                 
          ->select('id','name')
          ->whereNotIn('id', DB::table('curses')->where('id_user', '=', $id)->pluck('user_id'))
          ->get();  
Manoj
  • 1,530
  • 2
  • 13
  • 18
0

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

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 06 '23 at 00:12