检索 updated_at 早于 created_at 且在 Laravel 中有 2 小时的记录 (eloquent)

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

我想计算 updated_at 比 created_at 早 2 小时的记录。

代码

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

不幸的是,这不起作用,因为 subHours(2) 用于碳,但希望您明白了。

查看

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

控制器

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'));
    }

有人知道怎么做吗?

使用filter()方法。使用您的代码逻辑:

$teLang = $kentekens->filter(function($i) {
    return $i->updated_at->gt($i->created_at->subHours(2));
});

使用这条语句"where updated_at is 2 hours older than created_at":

$teLang = $kentekens->filter(function($i) {
    return $i->updated_at->lt($i->created_at->subHours(2));
});

也许是这样?

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

    return view('layouts.dashboard', compact('kentekens', 'teLang'));
}
$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)