This goes back to the models. Its better handled at the model level than the view level. Trying to do it at the view level would be messy. Are you in a position to share more on the models you are using to fetch this data / relationships they have. From within the models - you can do a custom function called from a controller that will fetch the data and group it by hour / customer so that by the time you display to the view - its well formatted and all the view does is display the data.
Need a logical help in view.blade file
Guys i have a small updation in my old project milkfarm
I had a form which makes a sales entry.
and then a view.blade to view the whole sales entry.
the below image is my sales entry view
Refer: https://imgur.com/a/3b1jd5k
how my sales entry view loos like is date, time, customer name,milk sold, total rate.( i ommited few columns which is not needed)
so in the above mentioned image i showed a entry
date time Customer name litres rate
07-11-1018 am Malgudi 30 1020
07-11-2018 pm Malgudi 20 680
05-12-2018 am Malgudi 35 1190
05-12-2018 pm Malgudi 29 986
My customer name:Malgudi
He will buy milk in am or pm.. the above mentioned are the entries. in this format only i store the data and it is retrieved.
But what my client is expecting the view like the below image.
Refer: https://imgur.com/a/uiLblRT
My customer expecting like this. date must be title and customer name must be once . in date order the litres must be displayed. all the data are in table but the view i cant get the logic.
Kindly some one help please.Give me a idea about this.
Would you like a quick fix or would you like to implement a robust solution.
A quickfix would be doing a query to the sales tables - looping through the sales details and grouping the details with the hour
A robust solution would be breaking down your entities further. You currently have a "sales_details" entity - but from the information you are saving - I can see a customer entity (/Model / Object), sale entity, You could work with 2 tables a sales table and a customers table. Such an approach would allow you to move the "logic" as properties and methods to your model classes and look at them as objects. Quick fix looks at this as data - so you end up fetching data and putting all the logic in one controller
Back to the quick fix. It would go something like this
//doing the querry
$sales = Sales_details::orderBy('created_at','desc')->get();
$sales_details = [];
foreach ($sales as $key => $sale) {
//total litres initialisation for all customers
$sales_details[$sale->customer_name]["total_litres"] = 0;
//total rate initialisation for all customers
$sales_details[$sale->customer_name]["total_rate"] = 0;
}
foreach ($sales as $key => $sale) {
//array of customer names grouped by the customer id
$sales_details[$sale->customer_id]["name"] = $sale->customer_name;
//this will give you an array of times for the customer - you can use Carbon/carbon to format the time to get the am and pm - can explain further
$sales_details[$sale->customer_id]["time"][] = $sale->time;
//sum up the the total litres grouped by customer id
$sales_details[$sale->customer_id]["total_litres"] += $sale->total_litres;
//sum up the the total rate grouped by customer id
$sales_details[$sale->customer_id]["total_rate"] += $sale->total;
}
So think along those lines. Once you pass your sales_details to the views - its something you can loop through and get the respective fields you like.
Please or to participate in this conversation.