Laravel

laravel eloquent wherein example

Pinterest LinkedIn Tumblr

Sure, here’s an example of using the whereIn method in Laravel’s Eloquent ORM:use App\User; $users = User::whereIn('id', [1, 2, 3])->get();

In this example, the whereIn method is used to filter the users based on the id column. The method takes two arguments: the first is the name of the column to filter on, and the second is an array of values to filter by. The get method is used to retrieve all the users that matched the filter.

You can also use the whereIn method on multiple columns

use App\User;

$users = User::whereIn('id', [1, 2, 3])
    ->whereIn('email', ['[email protected]', '[email protected]'])
    ->get();

You also can use the whereNotIn method, this method filter the result by excluding the given values

use App\User;

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

In this example, the whereNotIn method is used to filter the users based on the id column and excluding the given values [1, 2, 3] . The method takes two arguments: the first is the name of the column to filter on, and the second is an array of values to filter by. The get method is used to retrieve all the users that matched the filter.

Hope this helps!

Write A Comment