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

haizone's avatar

get all manufacturer_id with groupby

i have a category model with products :

        public function products(){
             return $this->morphedByMany('App\Models\Product', 'categorized', 'categorized');
          }

and i have a products model with manufacturer by manufacturer_id in my db:

    public function manufacturer()
      {
            return $this->belongsTo('App\Models\Business', 'manufacturer_id')-  >where('manufacturer', true);
      }

how i can get a list for all the manufacturer belogs to products in this spesipic category

this how i go the product in the category:

$category->products()->get();

thanks

0 likes
2 replies
haizone's avatar

i do that but maybe have a better and fast option?

$manufacturers = new Collection;
    foreach ($category->products()->get() as $value) {
      $manufacturers->push($value->manufacturer);
    }

    $manufacturers = $manufacturers->unique()->all();

Please or to participate in this conversation.