简体   繁体   中英

How to display data from controller to blade in laravel?

I have this query in controller that output data as i want. I would like to display total to the view. Right now it shows total of the first order to all orders. for example if the first order total is $56, all orders will be $56. How can i show total of every order?

This is how the controller looks like

public function viewOrders(User $user)
{
//$seller = Auth::user();
$totals = OrderProduct::select("seller_id", DB::Raw("SUM(Subtotal) AS total"), 'order_id')
->where('seller_id', '=',  \Auth::user()->id)
->groupBy('seller_id')
->groupBy('order_id')
->get();

//dd($totals);


$orders = Order::whereHas('orderItems.product', function ($query) {
    $query->where('seller_id', '=',  \Auth::user()->id);
})->get();

//dd($orders);
return view('orders', ['orders'=> $orders, 'total'=> $totals] );
}

when i dd($totals) i get

Collection {#278 ▼
#items: array:4 [▼
0 => OrderProduct {#302 ▶}
1 => OrderProduct {#303 ▶}
2 => OrderProduct {#304 ▼
  #table: "order_product"
  #fillable: array:6 [▶]
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:3 [▶]
  #original: array:3 [▼
    "seller_id" => 1
    "total" => "56"------------------->I would like to show this
    "order_id" => 35
  ]
  #changes: []
}
3 => OrderProduct {#305 ▼
  #table: "order_product"
  #fillable: array:6 [▶
  #attributes: array:3 [▶]
  #original: array:3 [▼
    "seller_id" => 1
    "total" => "112"------------------->I would like to show this
    "order_id" => 36
  ]
  #changes: []
  #casts: []
}
]
}

Blade view

@foreach ($order->orderItems as $item)
@if($item->product->user_id == Auth::user()->id)

   <td>{{ $item->product->name }}</td>
   <td>{{ $item->product->price }}</td>

 @endif
 @endforeach

@foreach($total as $item)
 <td>Total: {{$item->total }}</td>
@endforeach

Your second foreach loop is likely the issue. You are in an HTML table. For each row , you are looping through the orderItems to make <td> s for the product, price, etc. It looks like you have a single column set up for the Total field for that item.

So, you are likely only able to write one item's total, since there is no more room on the table to add more <td> s. To see if this is the issue, you can perhaps attempt to put the totals into one <td> with a pipe in between as a test. This would go inside the first loop:

<td>
  @foreach($total as $item)
     {{$item->total }}
     {{ $loop->last?"":" | " }}
  @endforeach
</td>

If this is the case, you may want to re-architect for the 3 dimensional total per item - perhaps like the test above, or perhaps a single sum of the totals per product, etc. Something to allow for one row to contain one <td> for total(s).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM