Laravel 比较 where 语句中的时间戳
Laravel compare timestamps in where statement
我有一段代码,我在其中传递了一个 Carbon 日期,如下所示:
2017-08-18 22:53:50.031922
并想将其与某些记录的 created_at
时间戳进行比较。但是,似乎没有过滤掉记录; where 语句中的比较是否有效?
$test = Auth::user()->tests()->with([
'participants.testRecords' => function ($query) use ($latestCapture) {
$query->select('id', 'score', 'test_id', 'participant_id', 'capture_timestamp', 'score', 'created_at');
$query->where('test_records.created_at', '>', $latestCapture);
}])->findOrFail($id)->toArray();
如果 $latestCapture
是 instancej Carbon,你应该在这里使用:
$latestCapture->toDateTimeString()
确保您传递了有效的日期字符串。
还有一件事 - 你应该确保 created_at
填写在 PHP 而不是 MySQL 中(这是 Laravel 中的默认设置) - 如果当你在 PHP 和 MySQL
中有不同的时区时,你不能期待时差
我有一段代码,我在其中传递了一个 Carbon 日期,如下所示:
2017-08-18 22:53:50.031922
并想将其与某些记录的 created_at
时间戳进行比较。但是,似乎没有过滤掉记录; where 语句中的比较是否有效?
$test = Auth::user()->tests()->with([
'participants.testRecords' => function ($query) use ($latestCapture) {
$query->select('id', 'score', 'test_id', 'participant_id', 'capture_timestamp', 'score', 'created_at');
$query->where('test_records.created_at', '>', $latestCapture);
}])->findOrFail($id)->toArray();
如果 $latestCapture
是 instancej Carbon,你应该在这里使用:
$latestCapture->toDateTimeString()
确保您传递了有效的日期字符串。
还有一件事 - 你应该确保 created_at
填写在 PHP 而不是 MySQL 中(这是 Laravel 中的默认设置) - 如果当你在 PHP 和 MySQL