Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrive records where updated_at is older than created_at with 2 hours in Laravel (eloquent)

I want to count records where updated_at is 2 hours older than created_at.

Code

$teLang = $kentekens->where('updated_at', '>', 'created_at'->subHours(2));

Unfortunately this doesn't work because subHours(2) is for carbon but hopefully you get the idea.

View

<h1>{{ $teLang->count() }}</h1>

Controller

public function create() {
        $kentekens = Kenteken::latest()
            ->where('created_at', '>=', Carbon::today())
            ->get();

        $teLang = $kentekens->where('updated_at', '>', 'created_at'->subHours(2));

        return view('layouts.dashboard', compact('kentekens', 'teLang'));
    }

anyone know how?

like image 393
Jessey Fransen Avatar asked Oct 22 '25 17:10

Jessey Fransen


1 Answers

$kentekens->whereRaw('`updated_at` > DATE_ADD(`created_at`, INTERVAL 2 HOUR)');

SQL:

select `id` from `tablename` where `updated_at` > DATE_ADD(`created_at`, INTERVAL 2 HOUR)
like image 81
odan Avatar answered Oct 24 '25 06:10

odan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!