laraveleloquentlaravel-relations

facing difficulty on implementing "groupBy" method in laravel


I am trying to fetch data using groupBy method in laravel, but it's returning all data. what I am trying to get is

lets there are 2 tables

Table 1 : variants

id    variant
1     color
2     size

Table 2: product_variants

color   variant_id
red     1
yellow  1
red     1
sm      2
xl      2
lg      2

Now I want to fetch data so it returns as follow:

variant_table: {
  id:1,
  variant: color,
  variants: {
    variant_id: 1,
    color:red
  },
  {
    variant_id: 1,
    color:yellow
  }
},

{
  id:2,
  variant: size,
  variants: {
    variant_id: 2,
    color:sm
  },
  {
    variant_id: 2,
    color:lg
  },
  {
    variant_id: 2,
    color:xl
  }
}

But I am getting all variants instead of distincts grouped by variant_table id, My code:

$productVariants  = ProductVariant::with('productVariants')
    ->whereHas('productVariants',function ($q) {
        $q->groupBy('variant_id');
    })
    ->get();
  

Solution

  • let suppose the model of product_variants table is ProductVariant let suppose the model of variants table is Variant let suppose the in model ProductVariant the relation function name is variant()

    $product_variants = ProductVariant::with('variant')->groupBy('variant_id')->get();
    

    try this $product_variants = ProductVariant::with('variant')->get()->groupBy('variant_id'); if it does not work than config\database.php --> "mysql" array Set 'strict' => false