Laravel whereIn & whereNotIn Eloquent Query with Example

May 05, 2021 . Admin

Hello Friends,

Now let's see example of how to use whereIn() and whereNotIn() in laravel. This is a short guide on laravel if whereIn(), whereNotIn(). We will use how to use whereIn() and whereNotIn() in laravel. Here you will learn how to use whereIn() and whereNotIn() in laravel. Let's get started with how to whereIn() and whereNotIn() use in laravel.

Here i will give you many example and understand how works this whereIn and whereNotIn functions one by one in laravel.

Example whereIn():

The whereIn method verifies that a given column's value is contained within the given array.

/**
    * Write code on Method
    *
    * @return response()
*/
public function index()
{
    $users = User::whereIn('id', [1, 2])
            ->get();
    dd($users);
}
Output:
Illuminate\Database\Eloquent\Collection {#674 ▼
  #items: array:2 [▼
    0 => App\Models\User {#1042 ▼
      #fillable: array:4 [▶]
      #hidden: array:2 [▶]
      #casts: array:1 [▶]
      #connection: "mysql"
      #table: "users"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:9 [▶]
      #original: array:9 [▼
        "id" => 1
        "name" => "admin"
        "email" => "admin@gmail.com"
        "email_verified_at" => null
        "password" => "$2y$10$felGOYT3p2AhMK0liGoDnO/j/QHTavk7zKN2zZBAiHaU.JfLiUkxu"
        "remember_token" => null
        "created_at" => "2021-03-03 10:36:21"
        "updated_at" => "2021-04-26 12:33:42"
        "is_active" => 1
      ]
      #changes: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #visible: []
      #guarded: array:1 [▶]
      #rememberTokenName: "remember_token"
    }
    1 => App\Models\User {#1289 ▼
      #fillable: array:4 [▶]
      #hidden: array:2 [▶]
      #casts: array:1 [▶]
      #connection: "mysql"
      #table: "users"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:9 [▶]
      #original: array:9 [▼
        "id" => 2
        "name" => "Mack Reinger II"
        "email" => "garnet69@example.org"
        "email_verified_at" => "2021-04-13 11:19:52"
        "password" => "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi"
        "remember_token" => "mP21KOhqgT"
        "created_at" => "2021-04-13 11:19:52"
        "updated_at" => "2021-04-13 11:19:52"
        "is_active" => 1
      ]
      #changes: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #visible: []
      #guarded: array:1 [▶]
      #rememberTokenName: "remember_token"
    }
  ]
}
Example whereNotIn():

The whereNotIn method verifies that the given column's value is not contained in the given array.

/**
    * Write code on Method
    *
    * @return response()
*/
public function index()
{
    $users = User::whereNotIn('id', [1, 2,3, 4])
            ->get();
    dd($users);
}
Output:
Illuminate\Database\Eloquent\Collection {#1302 ▼
  #items: array:17 [▼
    0 => App\Models\User {#1303 ▼
      #fillable: array:4 [▶]
      #hidden: array:2 [▶]
      #casts: array:1 [▶]
      #connection: "mysql"
      #table: "users"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:9 [▶]
      #original: array:9 [▼
        "id" => 5
        "name" => "Yessenia Herzog"
        "email" => "qward@example.org"
        "email_verified_at" => "2021-04-13 11:19:52"
        "password" => "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi"
        "remember_token" => "AwAEjhR35h"
        "created_at" => "2021-04-13 11:19:53"
        "updated_at" => "2021-04-13 11:19:53"
        "is_active" => 1
      ]
      #changes: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #visible: []
      #guarded: array:1 [▶]
      #rememberTokenName: "remember_token"
    }
    1 => App\Models\User {#1304 ▼
      #fillable: array:4 [▶]
      #hidden: array:2 [▶]
      #casts: array:1 [▶]
      #connection: "mysql"
      #table: "users"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:9 [▶]
      #original: array:9 [▼
        "id" => 6
        "name" => "Savanah Langworth II"
        "email" => "jazlyn95@example.net"
        "email_verified_at" => "2021-04-13 11:19:52"
        "password" => "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi"
        "remember_token" => "guubVlxBHf"
        "created_at" => "2021-04-13 11:19:53"
        "updated_at" => "2021-04-13 11:19:53"
        "is_active" => 1
      ]
      #changes: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #visible: []
      #guarded: array:1 [▶]
      #rememberTokenName: "remember_token"
    }
  ]
}

It will help you....

#Laravel