我想获取与今天日期的日期差异,并在查询数据库每行以计算天数时设置为引用。如何以 laravel-eloquent 的方式构建它?谢谢!
$query ="SELECT unix_timestamp(NOW()) - unix_timestamp(created_at) AS time_diff from users WHERE trial=1";
$result=mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_assoc($result))
{
$diff= $row['time_diff']
}
最佳答案
您可以在laravel Query Builder中使用selectRaw
DB::table('users')
->selectRaw('unix_timestamp(NOW()) - unix_timestamp(created_at) AS time_diff')
->where('trial',1)
->get();
或者你想使用Eloquent
Model::select(DB::raw('unix_timestamp(NOW()) - unix_timestamp(created_at) AS time_diff'))
->where('trial',1)
->get();
关于php - Laravel 5.2 使用 Eloquent 方法获取每行数据的 date_diff,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56511805/