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

AbdulBazith's avatar

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.

0 likes
17 replies
douglasakula's avatar

@abdulbazith

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.

AbdulBazith's avatar

@douglasakula thank you for your response.

this is my migration for sales_details. dont consider the type, location, area, booth.


Schema::create('sales_details', function (Blueprint $table) {
            $table->increments('id');
            $table->string('sales_id');
            $table->date('date');
            $table->string('time');
            $table->string('customer_id');
            $table->string('customer_name');
            $table->string('customer_type');
            $table->string('customer_location');
            $table->string('customer_area');
            $table->string('customer_booth');
            $table->string('emp');
            $table->double('no_of_litre');
            $table->double('rate_per_litre');
            $table->double('total');
            $table->longText('note');
            $table->timestamps();
        });

and iam not using any relationships.

this is my Sales_details model

class Sales_details extends Model
{

    protected $dates = [
        'created_at',
        'updated_at',
        'date'
    ];

    public function getFormattedDateAttribute()
    {
        return $this->date->format('d-m-Y');

    }
    protected $appends = ['formatted_date'];

}

and i have a nav bar view sales entry. if i click that.

it moves to index method in sales_details controller

 public function index()
    {

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

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

        if($sales->lastPage() == $sales->currentPage())
        {
            View::share('grandTotal', $all->sum('no_of_litre'));
            View::share('grandTotal1', $all->sum('total'));
        }

        return view('Sales_details.view', [
            'sales' => $sales
        ]);

     
    }


in this index controller what i did is jest fetching all records and sotred in that $all variable for total. and for pagination wise total i gave $sales.

this what i have done. no relationships nothing i used.

just inserted the record in the sales_table and the view it.

my client expecting the view in the above mentioned format thats it. . refer: https://imgur.com/a/uiLblRT

but i kept is

refer

https://imgur.com/a/3b1jd5k --- in this format i kept

Kindly need help

douglasakula's avatar
Level 15

@abdulbazith

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.

AbdulBazith's avatar

@douglasakula thank you thank you so much for your clear explanation.

And very sorry for these delay . i stuck into another error in another project. so only late. very sorry

The last two lines

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

showed error Undefined index: total_litres

so i changes those tow lines as

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

then it worked. just i gave a dd($sales_details); and checked. the screen looked like this

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

The specific name and their total milk with total amount is displayed

and based on id the am and pm is displayed.

this is my controller

 return view('Sales_details.date_wise_view')->withSales_details($sales_details);


But in my view if i give

 @foreach($sales_details as $sales_detail)
            <tr>
            <td></td>
            </tr>
            @endforeach


it showing error Undefined variable: sales_details

whats the problem.

the names in that arrary must be display line by line in rows. and their total milk next the the name column. like that.

how to display the name, total litres, total_rate. time, cistomer_id everthing..

Kindly help please

AbdulBazith's avatar

Some one reply for me . please help to solve my problem.

how should i pass sales_details in view

realrandyallen's avatar

@ABDULBAZITH - Reference your variable in your view like this:

@foreach($salesDetails as $sales_detail)
    <tr>
        <td></td>
    </tr>
@endforeach
AbdulBazith's avatar

@realrandyallen thank you thank you for your response.

sorry for the delay, i was struck in another project.

s it worked.

but whats my mistake?? i have declared as $sales_details but how i can give $salesDetails

and now i need to display the name means what should do?

i need to display the name, id, total litres, total rate, and then the dates without duplicate how it is possible.

that is as i said malgudi is my customer so he bought milk from me on date see below

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


see on 07-11 itself he bought in am and pm,

so what i need to get is Refer: https://imgur.com/a/uiLblRT

7 am this much litres pm this much litres. so total

how can i do this.

Kindly reply.

i know that everything iam asking in this. sorry that i cant get any idea about that.

thats my problem.

realrandyallen's avatar

@ABDULBAZITH - You should be able to access the data like this:

foreach($salesDetails as $customerName => $details) {
    print $customerName . " - " . $details['total_litres'];
}
AbdulBazith's avatar

@realrandyallen

i tired all the combinations like

 @foreach($salesDetails as $customerName => $details)
            <tr>
                <td>{{ $details['total_litres'] }}</td>
              
            </tr>

        @endforeach


whatever i give it saying Undefined index: total_litres

Undefined index: customer_id

Undefined index: customer_name

what can i do.

actually how i can know that what are in that array how to view it.

@douglasakula If you are free please reply..

1 like
realrandyallen's avatar

@ABDULBAZITH - Do a @dd($salesDetails) and post the results so we can see the structure of the array and we'll be able to see how we should loop through it

1 like
AbdulBazith's avatar

@realrandyallen

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

This was the screen shot of that @dd($salesDetails)

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 [▶]
]



i put as image and also copied that content also

1 like
realrandyallen's avatar

The loop I provided should work for customer name and total_litres but you do not havecustomer_id and customer_name in the array - that's why you're getting Undefined index errors.

You also seem to have other customer data (like the times) in separate indexes of the array

1 like
AbdulBazith's avatar

@realrandyallen

how should i loop it weather like this

 @foreach($salesDetails as $customerName => $details)
            <tr>
                <td>{{ $details['total_litres'] }}</td>
              
            </tr>

        @endforeach



lie this??

AbdulBazith's avatar

@realrandyallen is the format i mentioned correct?

Kindly reply please..

sorry for delay, working with another project so only.

AbdulBazith's avatar

@douglasakula thank you for you good response.

i have made the question with another post, kindly if possible answer please.

Please or to participate in this conversation.