SmallDreams's avatar

Search with 2 tables

So i have a product that has a category_id and i wanna search the product with the connected category from.

$products = Product::whereEncrypted('catagory', 'like', '%' . $this->searchCatagory . '%')->latest()->paginate($this->perPage);

this is what i have but of course that doesnt work because the Category is not in the Product table onlt th category_id is.

Could someone please help me understand how i can still search all product like that?

0 likes
1 reply
tisuchi's avatar
tisuchi
Best Answer
Level 70

@smalldreams If you have a proper relationship, you can achieve this:

$products = Product::whereHas('category', function ($query) {
    $query->where('name', 'like', '%' . $this->searchCategory . '%');    //Assuming the category's field you're searching by is named 'name'
})->latest()->paginate($this->perPage);
2 likes

Please or to participate in this conversation.