Laravel where In Eloquent Query Example

Laravel where In Eloquent Query Example

Laravel provides various eloquent methods. In this laravel where in query with column example tutorial, you will how to use laravel eloquent whereIn() method to building a query with laravel model and query builder.

This tutorial provides you simple examples of laravel wherein method on query builder and model with columns. And as well as how to use laravel eloquent wherein with arrays.

The following syntax represents the whereIn eloquent method in laravel:

whereIn(Coulumn_name, Array);

Here,

  • Column_name:- Your database table column name.
  • Array: – array with comma-separated values.

Let’s take look at some example of whereIn() eloquent method in laravel:

Example 1: wherein Query Using Simple SQL Query:

SELECT *  FROM users  WHERE id IN (10, 15, 18) 

In this query, the data will be get from the DB table. Whose ids will be 10, 15, 18.

Example 2: Eloquent Wherein Query Using Query Builder

public function index()
{
    $data = DB::table('users')
                ->whereIn('id', [10, 15, 18])
                ->get();
  
    dd($data);                    
}

Example 3: Eloquent Wherein Query Using Laravel Model

public function index()
{
    $data= User::whereIn('id', [10, 15, 18])->get();
  
    dd($data);                    
}

So far you have seen in the given example. Have used ids for getting data into db table. But now for example 4, we will fetch the data by using the DB table column name.

Example 4: Eloquent Wherein Query Using Laravel Model With Different Column Name

public function index()
{
    $data= User::whereIn('name', ['john','dam','smith'])->get();
  
    dd($data);                    
}

Conclusion

Laravel wherein query example tutorial, you have learned how to use laravel wherein() method with eloquent query builder and model.

Recommended Laravel Posts

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *