title: How to add variable to mapToGroups in laravel
body: <p>I have this query :</p>
  
  <pre><code>$options = $product-&gt;options-&gt;mapToGroups(function ($item, $key) {
    return [$item-&gt;group-&gt;type =&gt; $item];
  });
  </code></pre>
  
  <p>It returns my product options with with their group types,</p>
  
  <h3>Overview</h3>
  
  <p><code>dd code above</code></p>
  
  <pre><code>Collection {#3138 ▼
    #items: array:3 [▼
      "color" =&gt; Collection {#3132 ▼
        #items: array:2 [▶]
      }
      "dropdown" =&gt; Collection {#3133 ▼
        #items: array:2 [▶]
      }
      "radio" =&gt; Collection {#3134 ▼
        #items: array:3 [▶]
      }
    ]
  }
  </code></pre>
  
  <p>As you see this product options are grouped in 3 types <code>color, dropdown &amp; radio</code></p>
  
  <p>Till this step everything is OK but I also need to get those group <code>Titles</code> in order to show the title above each group of options.</p>
  
  <h1>Screenshot</h1>
  
  <p><a href="https://i.stack.imgur.com/CJXh6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CJXh6.png" alt="one"></a></p>
  
  <h1>Code</h1>
  
  <p><code>Controller</code></p>
  
  <pre><code>$options = $product-&gt;options-&gt;mapToGroups(function ($item, $key) {
    return [$item-&gt;group-&gt;type =&gt; $item];
  });
  </code></pre>
  
  <p><code>Blade</code></p>
  
  <pre><code>@foreach($options as $group =&gt; $option)
    @if($group == 'color')
      {{$group}}: //group type (must replace with group title)
      @foreach($option as $opt) //looping options
        &lt;option&gt;{{$opt-&gt;title}}&lt;/option&gt;
      @endforeach
    @elseif($group == 'dropdown')
      // same loop as above happens here...
    @else
      // same loop as above happens here...
    @endif
  @endforeach
  </code></pre>
  
  <h3>Note:</h3>
  
  <p>As my group types are fixed <code>Only color,dropdown &amp; radio</code> so I will need this type for my loops <code>if conditions</code> I just need to somehow add their titles in my query because titles are dynamic can be anything.</p>
  
  <h2>more...</h2>
  
  <p><code>Example</code> I can have dropdown option with title <code>color</code> as well as <code>size</code> or anything else so my type is same <code>dropdown</code> but titles and options under that are different.</p>
  
  <h1>Question</h1>
  
  <ol>
  <li>How to place my option groups title in my query?</li>
  </ol>
  
  <p><em>PS: in case you need more code please let me know I will share.</em></p>
  
  <h1>Update</h1>
  
  <p>Here is the structure of my products options and where titles and data comes from:</p>
  
  <p><code>Product Model</code></p>
  
  <pre><code>public function options(){
    return $this-&gt;belongsToMany(Option::class, 'product_options', 'product_id', 'option_id');
  }
  </code></pre>
  
  <p><code>Option Model</code>  This is my product options</p>
  
  <pre><code>public function group(){
    return $this-&gt;belongsTo(OptionGroup::class, 'option_id');
  }
  
  public function products(){
    return $this-&gt;belongsToMany(Product::class);
  }
  </code></pre>
  
  <p><code>OptionGroup Model</code> this is where titles of each option groups come from</p>
  
  <pre><code>public function values(){
    return $this-&gt;hasMany(Option::class, 'option_id');
  }
  </code></pre>
  
  <p>The models above behave like this:</p>
  
  <pre><code>Option Group = Color
  Option = Red, Blue, Yellow (under color)
  Product = Color-&gt;[red, blue, yellow]
  </code></pre>
  
  <h1>Update 2</h1>
  
  <pre><code>Collection {#3221 ▼
    #items: array:3 [▼
      "color" =&gt; Collection {#3215 ▼
        #items: array:2 [▼
          0 =&gt; Option {#2859 ▼
            +timestamps: false
            #fillable: array:7 [▶]
            #connection: "mysql"
            #table: "options"
            #primaryKey: "id"
            #keyType: "int"
            +incrementing: true
            #with: []
            #withCount: []
            #perPage: 15
            +exists: true
            +wasRecentlyCreated: false
            #attributes: array:7 [▶]
            #original: array:9 [▶]
            #changes: []
            #casts: []
            #dates: []
            #dateFormat: null
            #appends: []
            #dispatchesEvents: []
            #observables: []
            #relations: array:2 [▼
              "pivot" =&gt; Pivot {#2869 ▶}
              "group" =&gt; OptionGroup {#2922 ▼
                +timestamps: false
                #fillable: array:2 [▶]
                #connection: "mysql"
                #table: "option_groups"
                #primaryKey: "id"
                #keyType: "int"
                +incrementing: true
                #with: []
                #withCount: []
                #perPage: 15
                +exists: true
                +wasRecentlyCreated: false
                #attributes: array:3 [▼
                  "id" =&gt; 1
                  "title" =&gt; "COLORS"
                  "type" =&gt; "color"
                ]
                #original: array:3 [▶]
                #changes: []
                #casts: []
                #dates: []
                #dateFormat: null
                #appends: []
                #dispatchesEvents: []
                #observables: []
                #relations: []
                #touches: []
                #hidden: []
                #visible: []
                #guarded: array:1 [▶]
              }
            ]
            #touches: []
            #hidden: []
            #visible: []
            #guarded: array:1 [▶]
          }
          1 =&gt; Option {#2860 ▶}
        ]
      }
      "dropdown" =&gt; Collection {#3216 ▶}
      "radio" =&gt; Collection {#3217 ▶}
    ]
  }
  </code></pre>