In this tutorial you will learn about the Laravel where In Eloquent Query Example and its application with practical example.
In this laravel where in query with column example tutorial, I’ll show you how to use laravel eloquent whereIn() method to fetch data from laravel model using laravel query builder.
Laravel where In Eloquent Query Example
In this article, we will provides you simple example to demonstrate the use of laravel wherein method on query builder and mode.We will also use example to show how to use laravel eloquent wherein with arrays.
where In method syntax
1 |
whereIn(coulumn_name, Array); |
Here,
column_name:- Your database table column name.
Array: – array with comma-separated values.
wherein Query Using Simple SQL Query:
1 |
SELECT * FROM users WHERE id IN (10, 15, 18) |
The above query will be get data for ids 10, 15, 18 from users table.
Eloquent Wherein Query Using Query Builder
In this example we will be fetching users data using laravel whereIn clause with query builder.
1 2 3 4 5 6 7 8 |
public function index() { $data = DB::table('users') ->whereIn('id', [10, 15, 18]) ->get(); dd($data); } |
Eloquent Wherein Query Using Laravel Model
In this example we will be fetching users data using laravel whereIn clause with laravel model.
1 2 3 4 5 6 |
public function index() { $data= User::whereIn('id', [10, 15, 18])->get(); dd($data); } |
Eloquent Wherein Query Using Laravel Model With Different Column Name
In this example we will be fetching users data using laravel whereIn clause with column names.
1 2 3 4 5 6 |
public function index() { $data= User::whereIn('name', ['john','dam','smith'])->get(); dd($data); } |