Answer the question
In order to leave comments, you need to log in
Laravel and data into relationships?
Goodnight! I have the following task.
To the front, you need to display a table with three levels of nesting
1. Customer
2. Amount (Total amount of all orders)
2.1 Order number
2.2 Order amount (Amount of cost of goods)
3.1 Name
3.2 Price
3.3 Quantity
3.4 Amount
For this I have the following database structure (Missing some fields)
Users
1.id
2.name
penalty_orders
1.id
2.user_id
3.order_id
penalty_products
1.id
2.qty
3.product_id
First, I make a selection with a conditionClient::whereHas('relatedOrders')
Thus, I get clients whose penalty_orders table is not empty. Also I deduce them in representation, but already here also there is a problem. Because I have to display the values for a particular user.
@foreach($clients as $client)
Клиент: {{ $client->first_name }} {{ $client->last_name }}
@foreach ($client->relatedOrders as $order)
@foreach ($order->relatedProducts as $product)
@php($a[] = $product->qty*$product->relatedPenalty->price)
@endforeach
@endforeach
Сумма: {{ array_sum($a) }}
@endforeach
$clients = Client::whereHas('relatedOrders')->with('relatedOrders.relatedProducts.relatedPenalty','relatedOrders.relatedProducts.relatedProduct')->latest()->get();
Answer the question
In order to leave comments, you need to log in
In the code, it's great for a person not to go through this
@foreach ($client->relatedOrders as $order)
@foreach ($order->relatedProducts as $product)
@php($a[] = $product->qty*$product->relatedPenalty->price)
@endforeach
@endforeach
in representation At Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question