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

davy_yg's avatar
Level 27

query

Hello,

I am trying to analize this related table.

ProductCatalogsRepository.php

public function listAllCatalog()
   {
    $query = $this->model->where('flag_product', FlagEnum::CATALOG)->latest();

    if (request('search')) {
     
        $keyword = request('search');
        $query = $query->whereTranslationLike('name', '%' . request('search') . '%');
                 $query->where(function ($q) {
                     $q->where('flag_product', '=', FlagEnum::CATALOG);
                 });
        $query->orWhereHas('productCategory', function ($q) use ($keyword) {
            $q->whereTranslationLike('name', '%' . $keyword . '%');
        });
        $query->orWhereHas('price', function ($q) use ($keyword) {
            $q->whereLike('sku', '%' . $keyword . '%');
        });
    }

    return $query;
}

I wonder where $q from? This query actually works.

0 likes
2 replies
tykus's avatar

The closure parameter for Query Builder methods such as whereHas etc. receives a Builder instance as a parameter.

You could source-dive the appropriate Query Builder code to see how it works; but generally you will see that the Builder method has a callable $callback parameter (the Closure) which is called with the $query, e.g.

return $callback($query, $type);

Please or to participate in this conversation.