In this article, we will see how to use the multiple where conditions in Laravel. You can use the multiple where conditions with Eloquent Model or Database Query Builder. After googling we have found many ways to use multiple where conditions. So let’s see useful & simple ways one by one with examples.
Laravel Multiple Where Conditions
Laravel provides an inbuilt where() method using that we can easily build our multiple where conditions query on Model or Database query builder.
You can write the where()
method with the following syntax:
where('column_name', 'operator', 'value')
Notes: If you don’t pass the operator then by default it will be considered = (equals to)
condition.
01 Example: Simple Laravel Multiple Where Conditions
$posts = Post::where('id', '=', 1)
->where('status', '=', 'publish')
->get();
The above example will produce the following SQL query:
select *
from posts
where id = 1 and status = 'publish'
02 Example: Pass Array As A Condition
$whereCondition = [
['status', '=', 'publish'],
['category', '=', 'tutorial'],
['date', '>=', '2020-01-01'],
['date', '<=', '2020-01-05'],
];
$posts = Post::where($whereCondition)->get();
Above example will produce the following SQL query:
select *
from posts
where status = 'publish' and category = 'tutorial' and date >= '2020-01-01' and date <= '2020-01-05'
03 Example: Chaining With where() & orWhere()
$user->posts()
->where('status', 'publish')
->orWhere('likes', '>=', 100)
->get();
Above example will generate the following SQL query:
select *
from posts
where user_id = ? and status = 'publish' or likes >= 100
04 Example: Complex Chaining With Parameters Grouping
use Illuminate\Database\Eloquent\Builder;
$user->posts()
->where(function (Builder $query) {
return $query->where('status', 'publish')
->orWhere('likes', '>=', 100);
})
->get();
The example above will produce the following SQL.
select *
from posts
where user_id = ? and (status = 'publish' or likes >= 100)
05 Example: Using Query Scopes
The Query Scopes is standard way of declaring additional conditions. It is very helpful to build more readable & reusable code.
You can define the scopes in your model by creating scopeFunctioname
. That means you need to prefix scope
word with the function
name. For example, you want to create a status
method then your scope will be scopeStatus
. Let’s see the example
app/Models/Post.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
/**
* Scope a query to only include popular posts.
* https://www.scratchcode.io
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeLikes($query)
{
return $query->where('likes', '>', 100);
}
/**
* Scope a query to only include publish posts.
* https://www.scratchcode.io
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeStatus($query)
{
return $query->where('status', 'publish');
}
}
After defining scope you can then use it as below:
$posts = Post::status()->likes()->get();
Additionally, read our guide:
- Laravel One To One Relationship Tutorial
- Best Way to Remove Public from URL in Laravel
- How To Print Or Debug Query In Laravel
- Specified Key Was Too Long Error In Laravel
- AJAX PHP Post Request With Example
- How To Use The Laravel Soft Delete
- How To Add Laravel Next Prev Pagination
- Laravel Remove Package With Example (Correct Way)
- Difference Between Factory And Seeders In Laravel
- Laravel: Increase Quantity If Product Already Exists In Cart
- How To Calculate Age From Birthdate
- How to Convert Base64 to Image in PHP
- Check If A String Contains A Specific Word In PHP
- How To Find Duplicate Records in Database
- How To Convert Word To PDF In Laravel
- Laravel CSRF Token Mismatch Error Message
That’s it for now. We hope this article helped you to learn Laravel multiple where conditions with examples.
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance. 🙂 Keep Smiling! Happy Coding!