从数据库中进行长时间的选择,得到比给定的unix时间戳更新的结果


Eloquent select from database, get results newer than a given unix timestamp

我想从数据库中选择比参数中指定的某个指定UNIX时间戳更新的行。

created_at是表tracks中的日期时间。参数$timestamp是一个UNIX时间戳(一个整数)。

所以我试着Tracks::where('created_at','>=',$timestamp)->get();,它只是返回所有轨道,无论我指定哪个$timestamp。

然而,我可以看到类型问题,我尝试过不同的方法,比如写"UNIX_TIMESTAMP(created_at)"而不是created_at,但没有成功。

所以我认为有一些优雅而简单的方法可以做到这一点。你能帮我吗?

有两种方法可以解决这个问题。

  1. 使用Tracks::where(DB::raw('UNIX_TIMESTAMP(created_at)'),'>=',$timestamp)
  2. 使用DateTimeCarbon对象

像这样:

$timestamp = Carbon'Carbon::createFromTimestamp($timestamp);
Tracks::where('created_at','>=',$timestamp)->get();