Laravel Eloquent "WHERE NOT IN"

ID : 20229

viewed : 38

Tags : laravellaravel-4eloquentlaravel

Top 5 Answer for Laravel Eloquent "WHERE NOT IN"

vote vote

99

Query Builder:

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

Eloquent:

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

81

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

vote vote

78

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(); 
vote vote

63

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(); 
vote vote

54

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(); 

Top 3 video Explaining Laravel Eloquent "WHERE NOT IN"

Related QUESTION?