2

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

16
  • What have you done to debug this? Have you tried print_r($products->suboptions)? Do color and size appear multiple times? Commented Jan 19, 2018 at 0:58
  • @BaileyParker what do you mean what i have done? i shared my code in question. here is my controller code $product = Product::where('slug', $slug)->firstOrFail(); Commented Jan 19, 2018 at 1:01
  • I mean SO isn't here to debug your code for you. You are expected to show to effort in debugging. In this case, I'd recommend print_r($product->suboptions) before the blade loop, so you can see if the suboptions are indeed duplicated. Commented Jan 19, 2018 at 1:04
  • @BaileyParker I shared dd result Commented Jan 19, 2018 at 1:07
  • in this line {{ $option->option->title }} where is option method refer to(what table)? Commented Jan 19, 2018 at 1:20

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

hallelujah :))), works just fine as i wanted, thank you so much.
One more thing how if i need to put comma in between except using dropdown? implode them?
Yeah, that would be the best I guess.
laravel.com/docs/5.5/collections#method-implode Just try $optioncollection->implode('id', ','); or whatever field you wanna implode over and with what syntax inbetween, if you wanna have titles, you could do $optioncollection->implode('title', ' - ') for example. In general I would suggest trying laravel.com/docs/5.5/collections#available-methods a little bit :)
1

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.

9 Comments

Undefined variable: product_id
public function options(){ return $this->belongsToMany(Option::class); } , public function suboptions(){ return $this->belongsToMany(Suboption::class, 'product_suboption', 'product_id', 'suboption_id'); }
@mafortis It is a concept. I mean your product Id. wait i will edit it.
@mafortis you should add belong to relations with exactly same method names in product model. this way first you access the sub option. after that the option that related to that product.
i get this error Call to undefined method Illuminate\Database\Query\Builder::suboption()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.