Laravel 5.2 使用 eloquent 方法获取每行的 date_diff 数据

Laravel 5.2 Get date_diff of data each rows using eloquent method

我想获取与今天日期的日期差异,并在查询数据库每一行以计算天数时设置为参考。如何以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();