Why are you making Eloquent queries inside your Blade template? Why are you summing a VARCHAR type column? If order_date is a Date, why do you use a VARCHAR column type?
Apr 3, 2023
6
Level 1
How to calculate total sales
I want to get total sales from completed orders. there is no error but I can't get the total sales when order is completed
here is my code
my order table migrate
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->integer('customer_id');
$table->string('order_date');
$table->string('order_status');
$table->string('total_products');
$table->string('sub_total')->nullable();
$table->string('vat')->nullable();
$table->string('invoice_no')->nullable();
$table->string('total')->nullable();
$table->string('payment_status')->nullable();
$table->string('pay')->nullable();
$table->string('due')->nullable();
$table->timestamps();
});
}
in my Controller
public function TodaySales(){
$date = date("d-m-Y");
$today = Order::where('order_date',$date)->get();
$orders = Order::where('order_status','complete')->get();
return view('backend.sales.today_sales',compact('today','orders'));
} // End Method
Blade file
@extends('layout.master')
@section('content')
<div class="content">
<!-- Start Content-->
<div class="container-fluid">
<!-- start page title -->
<div class="row">
<div class="col-12">
<div class="page-title-box">
<div class="page-title-right">
<ol class="breadcrumb m-0">
</ol>
</div>
<h4 class="page-title">Today Sales</h4>
</div>
</div>
</div>
<!-- end page title -->
{{-- calculate total daily sales --}}
@php
$date = date("d-m-Y");
$sales = App\Models\Order::where('order_date',$date)->sum('pay');
@endphp
{{-- end --}}
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<h4 class="header-title"> Today Sales </h4>
<h4 style="color:white; font-size: 30px;" align="center"> Total : ₵{{ $sales }}</h4>
<table id="basic-datatable" class="table dt-responsive nowrap w-100">
<thead>
<tr>
<th>Sl</th>
<th>Order Date</th>
<th>Payment Method</th>
<th>Invoice Number</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
@foreach($today as $key=> $item)
<tr>
<td>{{ $key+1 }}</td>
<td>{{ $item->order_date }}</td>
<td>{{ $item->payment_status }}</td>
<td>{{ $item->invoice_no }}</td>
<td>{{ $item->pay }}</td>
</tr>
@endforeach
</tbody>
</table>
</div> <!-- end card body-->
</div> <!-- end card -->
</div><!-- end col-->
</div>
<!-- end row-->
</div> <!-- container -->
</div> <!-- content -->
@endsection
Level 122
Start with improving the database schema, then everything else will be much easier.
For instance, if something is a number then store it as a number and then you can use sum in your database queries
Or, if something is a date then store it in a date column then you can more easily search by date and date ranges. With the current setup, it would be impossible to ask for last weeks sales for instance.
Please or to participate in this conversation.