ホーム > Laravel > How to Perform "A or B" Searches in Laravel【orWhere】
Laravel

How to Perform "A or B" Searches in Laravel【orWhere】

Thank you for your continued support.
This article contains advertisements that help fund our operations.

This article explains how to perform "A or B" searches in Laravel.

Introduction

What we are going to do this time is,

A or B data retrieval

For example,

Retrieve users where "ID is 1" or "ID is 10"

This allows us to fetch data where either condition is satisfied.

You can use the same syntax in both Laravel 6 and Laravel 11.

Conclusion (How to Retrieve Users with ID 1 or ID 10)

Controller

$users = User::where('id',1)
    ->orWhere('id',10)
    ->get();

By chaining where and orWhere like this,

you can retrieve two users with "ID 1" or "ID 10."

By applying this, it is possible to perform searches such as retrieving either category 1 or category 2.

orWhere with Relationships

Premise

I will write this assuming there is a one-to-many table.

larave where or

posts is one and tags are many.

//Post.php
public function tags()
{
    return $this->hasMany(Tag::class);
}

How to Write orWhere with Relationships

Now, let's retrieve posts where the title is "Laravel" or where the related tags contain "Laravel."

$string = 'Laravel';
$posts = Post::where('title',$string)
    ->orWhereHas('tags', function($query) use ($string) {
        $query->where('name', $string);
    })
    ->get();
dd($posts);

What Does use Mean Here?

use is used to access external scope variables within a closure (anonymous function).

Since the closure's scope cannot access external variables, you need to explicitly pass those variables using use.

$string = 'Laravel';
$posts = Post::where('title',$string)
    ->orWhereHas('tags', function($query) {
        $query->where('name', $string); // This will cause an undefined error without use.
    })
    ->get();
dd($posts);

Undefined variable $string

What is $query?

It is the argument of the callback function.

Since it is an argument, it could be anything like $q.

This time, since a query builder is passed, it is common to use a name like $query to make it clear that it is a query builder, which is also what is written in the documentation, so using $query is the safer option.

$string = 'Laravel';
$posts = Post::where('title',$string)
    ->orWhereHas('tags', function($q) { //$q can be used here
        $q->where('name', $string); //$q works here
    })
    ->get();

It may seem difficult at first, but I think it's fine to write according to the types in the documentation and remember how to do it.

For those who want to fully understand, researching anonymous functions and callback functions is a good idea.

Other Complex Conditions

When You Want to Search with AND

How to Perform Multiple AND Searches with where in Laravel

When You Want to Search for A and (B or C)

If you want to search for A and B or C, this article explains it.

How to Retrieve Complex Conditional Searches, A and (B or C) in Laravel

Summary

This article summarized how to perform "A or B" searches in Laravel.

I hope it serves as a reference for someone.

Please Provide Feedback
We would appreciate your feedback on this article. Feel free to leave a comment on any relevant YouTube video or reach out through the contact form. Thank you!