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

Norbertho's avatar

How to get products where category id

Hi, I have a products and categories and category_product tables. So i have hasMany and belongsToMany relationship between the Products and Categories. When the user clicks on the category then i send an axios request to the controller and I would like to return the products related to that category. I have something like this in my controller but I cant make it work:

    public function fetchproducts($catid){
        $products = Product::where('categories', function ($query) use ($catid){
            $query->where('id', $catid);
        })
        ->with('productimages')->orderBy('id', 'DESC')
        ->paginate(9);


        return $products;
    }
0 likes
6 replies
Norbertho's avatar

i have found the solution:

    public function fetchproducts($catid){
        $products = Product::whereHas('categories', function ($query) use ($catid){
            $query->where('categories.id', $catid);
        })
        ->with(['productimages', 'categories'])->orderBy('id', 'DESC')
        ->paginate(9);

    
        return $products;
    }
MichalOravec's avatar
Level 75

@norbertho You have many to many relationship between products and categories so you have to use belongsToMany in both models.

class Product extends Model
{
    public function categories()
    {
        return $this->belongsToMany('App\Category');
    }
}
class Category extends Model
{
    public function products()
    {
        return $this->belongsToMany('App\Product');
    }
}

Also you code could look like this

public function fetchProducts($categoryId){
    return Product::with([
        'productimages', 'categories'
    ])->whereHas('categories', function ($query) use ($categoryId) {
        $query->where('id', $categoryId);
    })->orderByDesc('id')->paginate(9);
}

Docs: https://laravel.com/docs/7.x/eloquent-relationships#many-to-many

By the way it's better when you named your variable's and method's name in the camelCase.

Norbertho's avatar

Thanks @michaloravec Yeah that was my bad a wrote it incorrectly in my question but of course i used belongsToMany on both models in my projects. However now as you can see i load productimges as well but i would like to load only the image with featured field is 1. and select only the image column. It works

$products = Product::whereHas('categories', function ($query) use ($id){
                $query->where('categories.id', $id);
            })
            ->with(['productimages' => function($q){
             $q->where('productimages.featured', '=', '1');
            }, 'categories'])->orderBy('id', 'DESC')
            ->paginate(9);

but when i try to select only the image column then it is not working

$products = Product::whereHas('categories', function ($query) use ($id){
                $query->where('categories.id', $id);
            })
            ->with(['productimages' => function($q){
             $q->where('productimages.featured', '=', '1')->select('image);
            }, 'categories'])->orderBy('id', 'DESC')
            ->paginate(9);
Norbertho's avatar

@michaloravec like this? it is not woking

$products = Product::whereHas('categories', function ($query) use ($id){
                $query->where('categories.id', $id);
            })
            ->with(['productimages' => function($q){
             $q->where('productimages.featured', '=', '1')->select(['id', 'image']);
            }, 'categories'])->orderBy('id', 'DESC')
            ->paginate(9);

Please or to participate in this conversation.