Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdulBazith's avatar

Problem in view.blade Need to know the format.

This question was already asked in another thread. but got half the soution. problem is with display. so asking as new thread .

Refer this for in another thread: https://laracasts.com/discuss/channels/laravel/need-a-logical-help-in-viewblade-file#reply=484685

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

This what his format he is expecting

Cus_name        05              07                  
            am      pm      am      pm      total_litres            total_rate

Malgudi     35      29      -       20          84              1500
            

This is what my client expecting.

and this question was already asked.

As per @douglasakula answer,

   //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_name]["total_litres"] += $sale->total_litres;

        //sum up the the total rate grouped by customer id

        $sales_details[$sale->customer_name]["total_rate"] += $sale->total;

      }


it worked fine.

if i give @dd($salesDetails);

it shows like this,

Refer: https://imgur.com/a/aXnim5N

array:137 [▼
  "Balajie" => array:2 [▼
    "total_litres" => 13.0
    "total" => 455.0
  ]
  "Johnpan" => array:2 [▼
    "total_litres" => 13.0
    "total" => 455.0
  ]
  "Muthuraj" => array:2 [▼
    "total_litres" => 29.0
    "total" => 1160.0
  ]
  "M.M Pannai" => array:2 [▶]
  "Paramasivam" => array:2 [▶]
  "Selvam" => array:2 [▶]
  "Selladurai" => array:2 [▶]
  "Sivanaiya" => array:2 [▶]
  "Murugan vadai" => array:2 [▶]
  "Murugan" => array:2 [▶]
  "Merchant" => array:2 [▶]
  "Karuppasamy" => array:2 [▶]
  "Kadai" => array:2 [▶]
  "Mariappan" => array:2 [▶]
  "Meera" => array:2 [▶]
  "Nelson" => array:2 [▶]
  "Murugan (arun bakry)" => array:2 [▶]
  "malgudi" => array:2 [▶]
  "Arun" => array:2 [▶]
  "manikandan" => array:2 [▶]
  "Jothi" => array:2 [▶]
  "Kani" => array:2 [▶]
  "Rathigamess" => array:2 [▶]
  "Ramashanthiran" => array:2 [▶]
  "Sekar Tea" => array:2 [▶]
  "Sekar" => array:2 [▶]
  "Lakshmi sankar" => array:2 [▶]
  "Rajtea" => array:2 [▶]
  "Muthu Tea" => array:2 [▶]
  "Muthuvilas" => array:2 [▶]
  "Ammaiappar" => array:2 [▶]
  "Ratha" => array:2 [▶]
  "Gogila" => array:2 [▶]
  "Bombay" => array:2 [▶]
  "Nanjil" => array:2 [▶]
  "Thamilan tea" => array:2 [▶]
  "Packiaraj" => array:2 [▶]
  "Victor" => array:2 [▶]
  "Viyakapparaj Tea" => array:2 [▶]
  "Pannaiyar Tea" => array:2 [▶]
  "Innacimuthu tea" => array:2 [▶]
  "Sinnaraj Tea" => array:2 [▶]
  "Peria Raj Tea" => array:2 [▶]
  "Philip tea" => array:2 [▶]
  "kapil" => array:2 [▶]
  "Selvakumar" => array:2 [▶]
  "Sinnaraj" => array:2 [▶]
  "Selvakumar 1" => array:2 [▶]
  "Robert" => array:2 [▶]
  "Sinnaraj 1" => array:2 [▶]
  "Ganaselvam" => array:2 [▶]
  "Calf" => array:2 [▶]
  "Aavin" => array:2 [▶]
  "Sankar" => array:2 [▶]
  "Aathi 2" => array:2 [▶]
  "pallack Road Tea" => array:2 [▶]
  "sahayam" => array:2 [▶]
  "Samithurai" => array:2 [▶]
  "Master Loorthu samy" => array:2 [▶]
  "Kannan" => array:2 [▶]
  "Aanjali" => array:2 [▶]
  "Krisnan kovil" => array:2 [▶]
  "Mariammal" => array:2 [▶]
  "Ponnusamy" => array:2 [▶]
  "Karrupasamy" => array:2 [▶]
  "Bus stand" => array:2 [▶]
  "Aathi 1" => array:2 [▶]
  "Vighnesh" => array:2 [▶]
  "Bombay battrai" => array:2 [▶]
  "SelvaRaj tea" => array:2 [▶]
  "C44" => array:2 [▼
    "name" => "Balajie"
    "time" => array:2 [▼
      0 => "am"
      1 => "pm"
    ]
  ]
  "C43" => array:2 [▼
    "name" => "Johnpan"
    "time" => array:3 [▼
      0 => "pm"
      1 => "am"
      2 => "am"
    ]
  ]
  "C24" => array:2 [▶]
  "C61" => array:2 [▶]
  "C51" => array:2 [▶]
  "C57" => array:2 [▶]
  "C56" => array:2 [▶]
  "C55" => array:2 [▶]
  "C54" => array:2 [▶]
  "C77" => array:2 [▶]
  "C71" => array:2 [▶]
  "C50" => array:2 [▶]
  "C49" => array:2 [▶]
  "C63" => array:2 [▶]
  "C38" => array:2 [▶]
  "C53" => array:2 [▶]
  "C1" => array:2 [▶]
  "C46" => array:2 [▶]
  "C45" => array:2 [▶]
  "C42" => array:2 [▶]
  "C41" => array:2 [▶]
  "C39" => array:2 [▶]
  "C36" => array:2 [▶]
  "C65" => array:2 [▶]
  "C37" => array:2 [▶]
  "C35" => array:2 [▶]
  "C34" => array:2 [▶]
  "C64" => array:2 [▶]
  "C31" => array:2 [▶]
  "C30" => array:2 [▶]
  "C29" => array:2 [▶]
  "C28" => array:2 [▶]
  "C27" => array:2 [▶]
  "C73" => array:2 [▶]
  "C4" => array:2 [▶]
  "C3" => array:2 [▶]
  "C5" => array:2 [▶]
  "C14" => array:2 [▶]
  "C8" => array:2 [▶]
  "C6" => array:2 [▶]
  "C12" => array:2 [▶]
  "C11" => array:2 [▶]
  "C7" => array:2 [▶]
  "C23" => array:2 [▶]
  "C19" => array:2 [▶]
  "C18" => array:2 [▶]
  "C76" => array:2 [▶]
  "C20" => array:2 [▶]
  "C68" => array:2 [▶]
  "C22" => array:2 [▶]
  "C75" => array:2 [▶]
  "C74" => array:2 [▶]
  "C67" => array:2 [▶]
  "C70" => array:2 [▶]
  "C66" => array:2 [▶]
  "C69" => array:2 [▶]
  "C15" => array:2 [▶]
  "C10" => array:2 [▶]
  "C21" => array:2 [▶]
  "C62" => array:2 [▶]
  "C60" => array:2 [▶]
  "C59" => array:2 [▶]
  "C52" => array:2 [▶]
  "C48" => array:2 [▶]
  "C32" => array:2 [▶]
  "C26" => array:2 [▶]
  "C2" => array:2 [▶]
]


how i can retrieve it in my view fine in tabulation structure as my client expectation kindly please help me

what i tired is, like below

 @foreach($salesDetails as $customerName => $details) {
                 {{ $customerName . " - " . $details['total_litres'] }}
            }

            @endforeach

it showed error

Undefined index: total_litres (View: 

Now what should i do please some one help.

if this problem solve my updation will be over

0 likes
9 replies
bobbybouwmann's avatar
Level 88

It seems that not all $details have a total_litres key in the array and that's why you get this error?

 @foreach($salesDetails as $customerName => $details)
    @isset($details['total_litres'])
        {{ $customerName . " - " . $details['total_litres'] }}
    @endisset
@endforeach

This should do the trick for now!

However it would be better to filter this data before you send it to your view. So let your controller figure out the correct array and return that!

AbdulBazith's avatar

@bobbybouwmann thank you thank you sooo much.. it worked

now i made it like this

Refer: https://imgur.com/a/W7Dq3jd

The first entry Malgudi. that entry i made manually its not from db.

But like that i need others to be.

see the am and pm column are empty. how i can fill. i cant get any idea.

See tha "Balajie" is the record from db. the total litres is13 and the total rate is 455.

but need to fill the am and pm.

See this image: https://imgur.com/a/lSAtV2D

On 05-12-2018 am balajie bought 8 litres milk and

on 05-12-2018 pm balajie bought 5 litres of mik.

how i can sort it and how i can display it . like this i need to display all the records.

please, Kindly help me..

bobbybouwmann's avatar

You already have the data to loop over it, so you can make any calculation or data based on the given array you already have.

bobbybouwmann's avatar

Well you're looping over an array which has arrays in it. So based on that you can decide what to show and what not. You can't suddenly combine array values in your loop that have another index. It seems that your data is a mix between different kind of objects.

So you first have to decide what kind of data you want to show, collect that data per row you want to show in the table and then return that to your view.

AbdulBazith's avatar

@bobbybouwmann thank you for your response

I understood, that is i have different objects.

ok then can i fetch the information with the help of the $sale

is it possible,

to be frank i cant get the coding, unable to frame the logic for it, even the below logic too

i done with the help of @douglasakula


//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_name]["total_litres"] += $sale->total_litres;

        //sum up the the total rate grouped by customer id

        $sales_details[$sale->customer_name]["total_rate"] += $sale->total;

      }

Iam so confused..

Kindly help please

bobbybouwmann's avatar

@abdulbazith Your data is a real mess! I don't understand how your data is connected to each other. For example you have items that start with some name/item name like Muthuraj which holds some data. But then there are also items starting the C43 which holds the am pm data. How are they connected? You need some way to connect these to show these results in a table.

AbdulBazith's avatar

@bobbybouwmann let me explain this coding line by line, so that i too can understand. and even sometimes if my understanding is wrong means u can correct me,

and sorry for delay, here 2 days leave fo a festival thats y.

Just fetching all the information in descending order from sales_table and storing in $sales

      $sales = Sales_details::orderBy('created_at','desc')->get();

Making an empty array

 $sales_details = [];

Grouping the customers and initializing total_litres and amount for that customers. so here what happens for each name total_litre and amount is initiated. based on the customer name it is initiated.

 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"] = 0;

      }

at this position the if i do dd($sales_details) the screen looks like this

array:70 [▼
  "Balajie" => array:2 [▼
    "total_litres" => 0
    "total" => 0
  ]
  "Johnpan" => array:2 [▼
    "total_litres" => 0
    "total" => 0
  ]
  "Muthuraj" => array:2 [▶]
  "M.M Pannai" => array:2 [▶]
  "Paramasivam" => array:2 [▶]
  "Selvam" => array:2 [▶]
  "Selladurai" => array:2 [▶]
  "Sivanaiya" => array:2 [▶]
  "Murugan vadai" => array:2 [▶]
  "Murugan" => array:2 [▶]
  "Merchant" => array:2 [▶]
  "Karuppasamy" => array:2 [▶]
  "Kadai" => array:2 [▶]
  "Mariappan" => array:2 [▶]
  "Meera" => array:2 [▶]
  "Nelson" => array:2 [▶]
  "Murugan (arun bakry)" => array:2 [▶]
  "malgudi" => array:2 [▶]
  "Arun" => array:2 [▶]
  "manikandan" => array:2 [▶]
  "Jothi" => array:2 [▶]
  "Kani" => array:2 [▶]
  "Rathigamess" => array:2 [▶]
  "Ramashanthiran" => array:2 [▶]
  "Sekar Tea" => array:2 [▶]
  "Sekar" => array:2 [▶]
  "Lakshmi sankar" => array:2 [▶]
  "Rajtea" => array:2 [▶]
  "Muthu Tea" => array:2 [▶]
  "Muthuvilas" => array:2 [▶]
  "Ammaiappar" => array:2 [▶]
  "Ratha" => array:2 [▶]
  "Gogila" => array:2 [▶]
  "Bombay" => array:2 [▶]
  "Nanjil" => array:2 [▶]
  "Thamilan tea" => array:2 [▶]
  "Packiaraj" => array:2 [▶]
  "Victor" => array:2 [▶]
  "Viyakapparaj Tea" => array:2 [▶]
  "Pannaiyar Tea" => array:2 [▶]
  "Innacimuthu tea" => array:2 [▶]
  "Sinnaraj Tea" => array:2 [▶]
  "Peria Raj Tea" => array:2 [▶]
  "Philip tea" => array:2 [▶]
  "kapil" => array:2 [▶]
  "Selvakumar" => array:2 [▶]
  "Sinnaraj" => array:2 [▶]
  "Selvakumar 1" => array:2 [▶]
  "Robert" => array:2 [▶]
  "Sinnaraj 1" => array:2 [▶]
  "Ganaselvam" => array:2 [▶]
  "Calf" => array:2 [▶]
  "Aavin" => array:2 [▶]
  "Sankar" => array:2 [▶]
  "Aathi 2" => array:2 [▶]
  "pallack Road Tea" => array:2 [▶]
  "sahayam" => array:2 [▶]
  "Samithurai" => array:2 [▶]
  "Master Loorthu samy" => array:2 [▶]
  "Kannan" => array:2 [▶]
  "Aanjali" => array:2 [▶]
  "Krisnan kovil" => array:2 [▶]
  "Mariammal" => array:2 [▶]
  "Ponnusamy" => array:2 [▶]
  "Karrupasamy" => array:2 [▶]
  "Bus stand" => array:2 [▶]
  "Aathi 1" => array:2 [▶]
  "Vighnesh" => array:2 [▶]
  "Bombay battrai" => array:2 [▶]
  "SelvaRaj tea" => array:2 [▶]
]

after that there made another for loop to total the litres and total the amount for that customer. here whats happens the total litres of milk is stores in total_litres for specific customer and the total amount of that customer is stored in total. based in the name this is happening

 foreach ($sales as $key => $sale)
      {
     
        $sales_details[$sale->customer_name]["total_litres"] += $sale->no_of_litre;

       
        $sales_details[$sale->customer_name]["total"] += $sale->total;
      }



after this if i give dd($sales_details)

it gives like this


array:70 [▼
  "Balajie" => array:2 [▼
    "total_litres" => 13.0
    "total" => 455.0
  ]
  "Johnpan" => array:2 [▼
    "total_litres" => 13.0
    "total" => 455.0
  ]
  "Muthuraj" => array:2 [▶]
  "M.M Pannai" => array:2 [▶]
  "Paramasivam" => array:2 [▶]
  "Selvam" => array:2 [▶]
  "Selladurai" => array:2 [▶]
  "Sivanaiya" => array:2 [▶]
  "Murugan vadai" => array:2 [▶]
  "Murugan" => array:2 [▶]
  "Merchant" => array:2 [▶]
  "Karuppasamy" => array:2 [▶]
  "Kadai" => array:2 [▶]
  "Mariappan" => array:2 [▶]
  "Meera" => array:2 [▶]
  "Nelson" => array:2 [▶]
  "Murugan (arun bakry)" => array:2 [▶]
  "malgudi" => array:2 [▶]
  "Arun" => array:2 [▶]
  "manikandan" => array:2 [▶]
  "Jothi" => array:2 [▶]
  "Kani" => array:2 [▶]
  "Rathigamess" => array:2 [▶]
  "Ramashanthiran" => array:2 [▶]
  "Sekar Tea" => array:2 [▶]
  "Sekar" => array:2 [▶]
  "Lakshmi sankar" => array:2 [▶]
  "Rajtea" => array:2 [▶]
  "Muthu Tea" => array:2 [▶]
  "Muthuvilas" => array:2 [▶]
  "Ammaiappar" => array:2 [▶]
  "Ratha" => array:2 [▶]
  "Gogila" => array:2 [▶]
  "Bombay" => array:2 [▶]
  "Nanjil" => array:2 [▶]
  "Thamilan tea" => array:2 [▶]
  "Packiaraj" => array:2 [▶]
  "Victor" => array:2 [▶]
  "Viyakapparaj Tea" => array:2 [▶]
  "Pannaiyar Tea" => array:2 [▶]
  "Innacimuthu tea" => array:2 [▶]
  "Sinnaraj Tea" => array:2 [▶]
  "Peria Raj Tea" => array:2 [▶]
  "Philip tea" => array:2 [▶]
  "kapil" => array:2 [▶]
  "Selvakumar" => array:2 [▶]
  "Sinnaraj" => array:2 [▶]
  "Selvakumar 1" => array:2 [▶]
  "Robert" => array:2 [▶]
  "Sinnaraj 1" => array:2 [▶]
  "Ganaselvam" => array:2 [▶]
  "Calf" => array:2 [▶]
  "Aavin" => array:2 [▶]
  "Sankar" => array:2 [▶]
  "Aathi 2" => array:2 [▶]
  "pallack Road Tea" => array:2 [▶]
  "sahayam" => array:2 [▶]
  "Samithurai" => array:2 [▶]
  "Master Loorthu samy" => array:2 [▶]
  "Kannan" => array:2 [▶]
  "Aanjali" => array:2 [▶]
  "Krisnan kovil" => array:2 [▶]
  "Mariammal" => array:2 [▶]
  "Ponnusamy" => array:2 [▶]
  "Karrupasamy" => array:2 [▶]
  "Bus stand" => array:2 [▶]
  "Aathi 1" => array:2 [▶]
  "Vighnesh" => array:2 [▶]
  "Bombay battrai" => array:2 [▶]
  "SelvaRaj tea" => array:2 [▶]
]


Am i right? is my understanding right?

Now everything is fine. i got the customer name, their total_litres and total_amount.

Now i need to display the am and pm based on date.

whats my doubt is

so just think of the $sales = Sales_details::orderBy('created_at','desc')->get(); which possesses all the rows and columns from the table.

so if there is a two rows,

17-01-2019 am Abdul 30 litres

17-01-2019 pm Abdul 50 litres.

consider this above two entries. i should change the above entry as


Customer                17-01-2019              litres  

Name                am          pm                  -           

Abdul               30          50                  80

That is the date must come one time only but the am pm must be displayed with the litres.

think the above example just having two rows. but it must be displayed given above.

How please give a glimpse.

just forget the coding which given for total_litres and total.

only think the above example, for that how we can write code. just for the two rows.

Kindly suggest me a logic

Please or to participate in this conversation.