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

ThinkingMan's avatar

Get count of rows by date greater than today

This seems like an easy one but maybe I am looking at the problem too simplistically. All I want to do is produce the count of 'delivery_date' days which have a 'delivery_date' greater than today. The $allDeliveries just produces a 0. If I addDays to today then I will get the delivery count for that day. So it is reading the data so my query is the problem.

    public function show()
    {
        $ID = auth()->user()->id;
        $date = Carbon::today()->toDateString();
        $today = Carbon::today();

        $userorders = DB::table('order_headers')->where('user_id', $ID);
        $ordercount = $userorders->where('status', 'active')->count();

        $usertickets = DB::table('ticketit')->where('user_id', $ID);
        $ticketcount = $usertickets->where('status_id', '1')->count();

        $today->modify('+5 days');
        $modified_date = $today->format('Y-m-d');

        $userorderstoday = DB::table('order_headers')->where('user_id', $ID);
        $deliveriesToday = $userorderstoday->where('delivery_date', '=', $today)->count();
        $allDeliveries = $userorderstoday->where('delivery_date', '>', $today)->count();

        return view('home', ['date' =>$date, 'ordercount' => $ordercount, 'allDeliveries' => $allDeliveries, 'modified_date' =>$modified_date, 'ticketcount' => $ticketcount, 'deliveriesToday' => $deliveriesToday]);
    }

You can ignore the $modified_date as I am just temporarily using to show output in my view,

Thanks in advance - Noob...

0 likes
7 replies
realrandyallen's avatar

Try doing it this way:

$deliveriesToday = $userorderstoday->whereDate('delivery_date', '=', now())->count();
$allDeliveries = $userorderstoday->whereDate('delivery_date', '>', now())->count();

// alternatively
$allDeliveries = $userorderstoday->whereDate('delivery_date', '>', now()->addDays(5))->count();
ThinkingMan's avatar

Getting 0 on both deliveriesToday and allDeliveries with now().

ThinkingMan's avatar

@vilfago the whereDate method is not found in the class. Here is what I am using. Do I need to add something?

namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\Facades\View;
Vilfago's avatar

Which version of laravel do you use ?

Is the delivery date a timestamp or a date field in db?

ThinkingMan's avatar

Laravel Spark version 5.0.1

It's a date field, not timestamp

ThinkingMan's avatar

And just a posted code solution that worked for anyone else who may read this:

public function show()
    {
        $ID = auth()->user()->id;
        $date = Carbon::today()->toDateString();
        $today = Carbon::today();

        $userorders = DB::table('order_headers')->where('user_id', $ID);
        $ordercount = $userorders->where('status', 'active')->count();

        $usertickets = DB::table('ticketit')->where('user_id', $ID);
        $ticketcount = $usertickets->where('status_id', '1')->count();

        $modified_date = Carbon::today()->addDay(5);
        
        $userorderstoday = DB::table('order_headers')->where('user_id', $ID);
        $deliveriesToday = $userorderstoday->whereDate('delivery_date', '=', $today)->count();
        
        $deliveryOrders = $userorders->where('delivery_date','>',$today)->where('delivery_date','<', $modified_date->endOfDay())->count();

        return view('home', ['date' =>$date, 'ordercount' => $ordercount, 'deliveryOrders' => $deliveryOrders, 'modified_date' =>$modified_date, 'ticketcount' => $ticketcount, 'deliveriesToday' => $deliveriesToday]);
    }
1 like

Please or to participate in this conversation.