how can I should read this data

Published 6 months ago by satiseven777

Please see the picture I want to read Order_products data,I,m getting all of other data in the array but order_product data is trouble for me!

https://drive.google.com/file/d/1UmzPO2HLNPKX-nmboMG23UqIHzM5mP5X/view?usp=sharing

Best Answer (As Selected By satiseven777)
lostdreamer_nl

This should work: In the view place this

And it makes life a lot easier when you name your relationships like what they are (products relation, even though the name is plural gives back a single product, but the orders_product relation (singular name) gives a collection....)

<ul>
@foreach($orders as $order)
    <li>
    <h3>Order: {{ $order->id }} </h3>
    <ul>
    @foreach($order->orders_product as $orderProduct)
        <li> {{ $orderProduct->products->id }} </li>
    @endforeach
    </ul>
    </li>
@endforeach
</ul>
satiseven777

and when I want to get this in foreach I got this error I use this foreach in other foreach that returns all of orders

mycode

$orders = orders::orderBy('id', 'desc')->with(['user.profile','OrdersProducts.products.images'])->get()->all();
 
        return view('admin.satislist')->with(compact('orders'));
Shawdow

Can you Show the controller code

satiseven777

my controller codes is too short !

$orders = orders::orderBy('id', 'desc')->with(['user.profile','OrdersProducts.products.images'])->get()->all();
 
        return view('admin.satislist')->with(compact('orders'));
Shawdow

try this once


$orders = orders::orderBy('id', 'desc')->with(['user.profile','OrdersProducts.products.images'])->get()->all();
 
        return view('admin.satislist',compact('orders'));

Nielson

How are you trying to access it? Shouldn't you be able to access it by doing this:

$order->orders_product->product_id

?

Edit: Ah, I overlooked the

return view('admin.satislist')->with(compact('orders'));

It should be either

return view('admin.satislist')->withOrders($orders);

or like Shadow wrote

return view('admin.satislist', compact('orders'));

satiseven777

It doesn't make diffrent,It,s the same function~

Nielson

Hm - wasn't aware that it worked.

Could you access it by doing this?

$order->orders_product->product_id
satiseven777

No,Unfortunately! I get this error

Trying to get property of non-object

Shawdow

You need get the order_product and product_id both in for each?? where is the for each loop??

satiseven777

for each is in the blade view and I need full info of products that ordered and now I got those but I have problems in showing theme!

Nielson

Try this:

@foreach($orders as $order)
    {{ $order->pay_method }}

    @foreach($order->orders_products as $orders_products)
        {{ $orders_products->product_id }}
    @endforeach
@endforeach     

There might be an easier way to do it, but this should do the trick.

Edit: typo in second foreach loop.

Shawdow

does your problem related to above thread ???

lostdreamer_nl

This should work: In the view place this

And it makes life a lot easier when you name your relationships like what they are (products relation, even though the name is plural gives back a single product, but the orders_product relation (singular name) gives a collection....)

<ul>
@foreach($orders as $order)
    <li>
    <h3>Order: {{ $order->id }} </h3>
    <ul>
    @foreach($order->orders_product as $orderProduct)
        <li> {{ $orderProduct->products->id }} </li>
    @endforeach
    </ul>
    </li>
@endforeach
</ul>
Nielson

Or this:

{{ $order->orders_products[0]->product_id }}

satiseven777

tnx for your help Guys!

Please sign in or create an account to participate in this conversation.