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

freemium's avatar

get list of available categories from the query

 $products = Product::when($request->search, function ($query, $search) {
            return $query->where(function ($query) use ($search) {
                $query->where('name', 'like', $searchs = "%{$search}%")
                    ->orWhere('description', 'like', $searchs)
                    ->orWhere('color', 'like', $searchs)
                    ->orWhere('size', 'like', $searchs);
            });
        })
            ->when($request->min_price || $request->max_price, function ($query) use ($request) {
                return $query->where(function ($query) use ($request) {
                    $query->whereBetween('price', [$request->min_price, $request->max_price]);
                });
            })
            ->when($request->category && $request->category != 'all', function ($query) use ($request) {
                return $query->whereHas('sub_category.category', function ($query) use ($request) {
                    $query->where('slug', $request->category);
                });
            })
            ->when($request->brand, function ($query) use ($request) {
                return $query->whereHas('brand', function ($query) use ($request) {
                    $query->where('slug', $request->brand);
                });
            })
            ->when($request->sub_category, function ($query) use ($request) {
                return $query->whereHas('sub_category', function ($query) use ($request) {
                    $query->where('slug', $request->sub_category);
                });
            });
$products=$products->get();

i want to get list of categories from above query how can i get while looping through products collections when i loop

    @foreach ($products as $product)
        <h1>{{ $product->sub_category->category->name }}</h1>
    @endforeach
0 likes
3 replies
SilenceBringer's avatar

@freemium firstly you need to eager load it

$products=$products->with('sub_category.category')->get();

then - it's based on your relationsh types. if product belongsTo sub_category, and sub_category belongsTO category - your statement is right

{{ $product->sub_category->category->name }}

if you have another type of relationships - show it

freemium's avatar

@SilenceBringer i have loaded the relationship as you mentions above

while looping the products same category can be printed many times {{ $product->sub_category->category->name }} i want to get list of distinct category name belongs to that product data , i this case if two product belongs to one category then one category will be printed two times but i want to print only one time

SilenceBringer's avatar
Level 55

@freemium you can get it in controller by filtering products

$products=$products->get();
$productCategories = $product->map(fn ($product) => $product->sub_category->category) // return category instead of product, so it's collection of categories now
	->unique('id') // select unique by id
	->sortBy('name'); // sort

then output it in blade

@foreach ($productCategories as $category)
	{{ $category->name }}
@endforeach

Please or to participate in this conversation.