In this laravel 12 tutorial, you will see how to use laravel 12 multiple orwhere condition eloquent example. This tutorial work for laravel 7 , laravel 8, laravel 9, laravel 10 ,laravel 11 and laravel 12.
We’ll look at two examples: one using a simple combination of where and orWhere, and another using multiple orWhere clauses grouped inside a where condition. This approach is especially useful when you need to apply several OR conditions together with other WHERE clauses.
What is orWhere in Laravel?
The orWhere method adds an SQL OR condition to your query, allowing for results that match any of the provided conditions. It accepts the same arguments as the where method and supports chaining for multiple OR filters.
Example 1: Simple Example Where and orWhere
Suppose a scenario where a posts can search in multiple columns (title, description,tags,). The following example demonstrates how to use multiple orWhere conditions with Eloquent.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
public function index()
{
$search = "Lara";
$posts = Post::select("*")
->where('title', 'LIKE', '%'.$search.'%')
->orWhere('description', 'LIKE', '%'.$search.'%')
->orWhere('tags', 'LIKE', '%'.$search.'%')
->get();
dd($posts);
}
}
The SQL generated:
select * from `posts` where `title` LIKE '%Lara%' or `description` LIKE '%Lara%'
Read Also : Laravel 12 Summernote Image Upload CRUD Example
Example 2: Multiple orWhere with Grouped where Condition
Often, you need to mix where (AND) and orWhere (OR) for more complex logic. Grouping is crucial to ensure logical correctness. Use closures to group OR statements together.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
public function index()
{
$search = "Lara";
$posts = Post::where('status', 'published')->where(function($query) use ($search){
$query->where('title', 'LIKE', '%'.$search.'%')
->orWhere('description', 'LIKE', '%'.$search.'%')
->orWhere('tags', 'LIKE', '%'.$search.'%');
})->get();
dd($posts);
}
}
This is SQL generated:
SELECT * FROM posts WHERE status = 'published' AND (title LIKE '%Lara%' OR description LIKE '%Lara%' OR tags LIKE '%Lara%');
Example 3: Multiple Grouped orWhere Condition
You may encounter queries where you want to have several AND/OR blocks. Here’s how to structure this:
$books = Book::where(function ($query) {
$query->where('genre', 'Fantasy')
->orWhere('genre', 'Education');
})
->where(function ($query) {
$query->where('type', 'book')
->orWhere('type', 'ebook');
})
->get();
The generated SQL:
SELECT * FROM books WHERE (genre = 'Fantasy' OR genre = 'Education')
AND (type = 'book' OR type = 'ebook');
Tips for Using Multiple orWhere in Eloquent
- Always group OR blocks inside a closure when mixing with AND conditions to avoid logical errors.
- For purely OR queries on multiple columns, chain orWhere as shown above.
- You can chain any combination of where, orWhere, and even nest closures for deep queries.
Conclusion
Using multiple orWhere conditions in Laravel Eloquent empowers you to build flexible, user-friendly search and filter features. Make sure to group your logic wisely, especially when combining with other conditions, to get accurate results.