php – How to use data form different collection to show in single table row having products and product options in different collection in blade file-ThrowExceptions

Exception or error:

So i am using laravel 5.4 and fetching Orders from orders table and each order has multiple products
which is stored in ordered_products table with orders_id as foreign Key both table,
Now some products have option variables like size and color stored in another table ordered_product_options.on order detail page i am fetching products releted to
the particular orders_id and options related to the each product.
The problem is i am able to display products in view page in a table using foreach
but i want to display the product options variable in the same row as the product.
some of the products single option and some have multiple options.
Please Help Me to display the product and options in a table.

This is My collections which is i am using in the View

"product" => Collection {#201 ▼
#items: array:1 [▼
  0 => {#223 ▼
    +"_id": 9
    +"created_at": "2020-02-27 17:06:40"
    +"updated_at": "2020-02-27 17:06:40"
    +"products_id": 52
    +"orders__id": 5
    +"name": "Cap"
    +"price": 90.0
    +"status": 1
    +"quantity": 1
    +"custom_product_id": "UN727"
  }
]

}

 "options" => array:1 [▼
0 => Collection {#220 ▼
  #items: array:2 [▼
    0 => {#225 ▼
      +"_id": 2
      +"created_at": "2020-02-27 17:06:40"
      +"updated_at": "2020-02-27 17:06:40"
      +"ordered_products__id": 9
      +"name": "Color"
      +"value_name": "Green"
      +"addon_price": 0.0
    }
    1 => {#226 ▼
      +"_id": 3
      +"created_at": "2020-02-27 17:06:40"
      +"updated_at": "2020-02-27 17:06:40"
      +"ordered_products__id": 9
      +"name": "Size"
      +"value_name": "Large"
      +"addon_price": 0.0
    }
  ]
}

]

 <table class="table">
        <thead>
                      <tr>
            <th>Product Name</th>
                @if(!empty($order['options']))
            <th>View Option Values</th>

            @endif
        </tr>
        </thead>
        <tbody>
             @foreach($order['product'] as $product)

        <tr>

            <td><strong>{{$product->name}}</strong></td>
         <td>
                                <a href="{{ route('order.edit',$product->_id) }}" class="btn btn-sm btn-icon btn-success"><i class="fa fa-eye"></i></a>
                            </td>
          </tr>

      @endforeach

        </tbody>
    </table>

Currently i am using a different page to show the product options is there any way to show the both products and options on the same page.Thanks in advance.

How to solve:

try this

$order=Order::with('products.options')->find($orderId);

in your blade

 <table class="table">
        <thead>
                      <tr>
            <th>Product Name</th>
                @if(!empty($order['options']))
            <th>View Option Values</th>

            @endif
        </tr>
        </thead>
        <tbody>
             @foreach($order->products as $product)

        <tr>

            <td><strong>{{$product->name}}</strong></td>
         <td>
                                <a href="{{ route('order.edit',$product->_id) }}" class="btn btn-sm btn-icon btn-success"><i class="fa fa-eye"></i></a>
                            </td>

                 @foreach($product->options as $option)
                     <td>{{$option->name}}:{{$option-> value_name}}</td>
                 @endforeach

              </tr>
      @endforeach

        </tbody>
    </table>

Answer:

I figured it out in the end i just compared the product_id in both the Collections

 <table class="table">
        <thead>
                      <tr>
            <th>Product Name</th>
            <th>Quantity</th>
            @if(!empty($order['options']))
            <th>Values</th>
            <th>Values</th>

            @endif
        </tr>
        </thead>
        <tbody>
             @foreach($order['product'] as $product)

        <tr>

            <td><strong>{{$product->name}}</strong></td>
            <td><strong>{{$product->quantity}}</strong></td>

                        @foreach($order['options'] as $opt)
                         @foreach($opt as $option)

                       @if($product->_id == $option->ordered_products__id)  
                 <td><strong>{{$option->name}}:{{$option->value_name}}</strong></td>
                     @endif
                          @endforeach  
                           @endforeach  
          </tr>

      @endforeach

        </tbody>
    </table>

hope it will help someone else in the future

Leave a Reply

Your email address will not be published. Required fields are marked *