mafortis
mafortis

Reputation: 7128

Laravel loop issue

I need help with looping my product options, this is what i have now:

shoot

What I want is simply get 2 row's only one for color and another for size and in dropdown in front of each one have all items.

here is my blade code:

<tbody>
  @foreach($product->suboptions as $option)
  <tr>
    <td style="width: 150px;">{{ $option->option->title }}</td>
    <td class="text-left">
       <select name="" id="">
         <option value="{{$option->id}}">{{$option->title}} - {{ number_format($option->price, 0) }}</option>
       </select>
    </td>
  </tr>
  @endforeach
</tbody>

UPDATE

my dd result of {dd($product->suboptions)}}

shoot2

Upvotes: 2

Views: 445

Answers (2)

Inuyaki
Inuyaki

Reputation: 1037

You can first group them together with the mapToGroups() function for a collection
https://laravel.com/docs/5.5/collections#method-maptogroups

$something = $product->suboptions->mapToGroups(function ($item, $key) {
    return [$item->option->title => $item];
});

You should dd() that to see what the output is and understand it.
After that you can just cycle through them with foreach, your blade should be something like

<tbody>
  @foreach($something as $optiontitle => $optioncollection)
  <tr>
    <td style="width: 150px;">{{ $optiontitle }}</td>
    <td class="text-left">
      <select name="" id="">
        @foreach($optioncollection as $suboption)
          <option value="{{$suboption->id}}">{{$suboption->title}} - {{ number_format($suboption->price, 0) }}</option>
        @endforeach
      </select>
    </td>
  </tr>
  @endforeach
</tbody>

Upvotes: 1

Salar Bahador
Salar Bahador

Reputation: 1494

For accomplishing that you need to get option first, and write a method to access options via products id or products object. so you can right something like this in products model:

   public function get_option($product_id){

       $sub_options = Product::whereId($product_id)->first()->suboptions()->get();

       $option = array();
       foreach($sub_options as $sub_option){
         $option[] = $sub_option->options()->get();
       }

    return $option;
  }

And then in view, you just call this method and put it in 2 foreach, one for option and the other for sub-option. like this code below:

<tbody>
  @foreach($product->get_option($product->id) as $key=>$value)
  <tr>
    <td style="width: 150px;">{{ $value->title }}</td>
     <td class="text-left">
       <select name="" id="">
         @foreach($value->suboptions() as $key=>$value2)
         <option value="{{$value2->id}}">{{$value2->title}} - {{number_format($value2->price, 0) }}</option>
         @endforeach
       </select>
    </td>
  </tr>
  @endforeach
</tbody>

I don't know whats your model methods but you can get the concept.

I Repeat again you should not copy my code exactly. just follow along with the concept of what am I saying.

But this time according to your eloquent methods. this code should work.

Upvotes: 1

Related Questions