Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

himala's avatar

How to search for an item with relationships (SOLVED)

I have Products, BranchInventories, and Branch models with hasMany relationships

My Product table has id, and name

and my BranchInventory table has product_id, branch_id

and my Branch table has city, and address

What I want to do is a search item, for example:

the search item is 'All Purpose Flour' and city is 'Manila'

Search for App\Product::where('name', 'LIKE', '%all purpose flour%') AND branch('city', 'Manila') ->get()

Can you please help me? I hope you can understand my question. Thanks in advance

0 likes
11 replies
himala's avatar

Product.php

class Product extends Model { protected $guarded = []; public function store() { return $this->belongsTo(Store::class); }

public function category()
{
    return $this->belongsTo(Category::class);
}

public function branch()
{
    return $this->hasMany(BranchInventory::class);
}

}

Branch.php

class Branch extends Model { protected $guarded = [];

public function store()
{
    return $this->belongsTo(Store::class);
}

public function branchInventory()
{
    return $this->hasMany(BranchInventory::class);
}

}

BranchInventory.php

class BranchInventory extends Model { protected $guarded = [];

public function branch()
{
    return $this->belongsTo(Branch::class);
}

public function product()
{
    return $this->belongsTo(Product::class);
}

}

himala's avatar

I want to use branchesInventory branch_id and product_id to connect with branches table

himala's avatar

THANK YOU! But am i doing this right?

    $city = 'Manila';
    foreach ($arr as $line) {
        $products[] = \App\Product::where('name', 'LIKE', '%' . $line . '%')
            ->whereHas('branch', function ($query) use ($city) {
                $query->whereHas('branch', function ($query) use ($city) {
                    $query->where('city', $city);
                });
            })
            ->get();
    }
MichalOravec's avatar

@himala Yes it's exactly same think, so

$search = 'all purpose flour';

$city = 'Manilla';

$products = App\Product::where('name', 'LIKE', "%{$search}%")->whereHas('branch', function($query) use ($city) {
    $query->where('city', $city);
})->get()

1 like
himala's avatar

Thank you so much!! I've been trying to figure this out for about 6 hours now. Thanks again!

MichalOravec's avatar

@himala I think your whole code should be like this

$city = 'Manilla';

$products = App\Product::where(function ($query) use ($arr) {
    foreach ($arr as $line) {
        $query->orWhere('name', 'LIKE', "%{$line}%");
    }      
})->whereHas('branch', function($query) use ($city) {
    $query->where('city', $city);
})->get()
1 like
himala's avatar

I tried to do that but it produces an error maybe because I have 'branch' function in my products that returns hasMany BranchInventory::class

which has the branch_id

and my BranchInventory has 'branch' function that returns hasMany 'Branch::class' so i can compare them with

branch_id from BranchInventory an the id from the Branch table. My naming convention is wrong

Please or to participate in this conversation.