我有 4 个单选按钮
,每个按钮对应一个时间段:
- 上周
- 上个月
- 最后一天
我想根据这些时间选择数据库记录。
例如,当用户选择上个月时,应该显示从上个月到现在的所有记录,但我的created_at 字段
不起作用。
你的想法是什么?
最佳答案
在您的 Record
类中,我将创建一个查询范围 ( http://laravel.com/docs/eloquent#query-scopes ):
public function scopeCreatedLastXDays($query, $nb)
{
return $query->where('created_at', '>=', new DateTime('-'.$nb.' days'));
}
然后我会检查输入:
if (Input::has('last_month'))
$records = Record::createdLastXDays(30)->get();
if (Input::has('last_week'))
$records = Record::createdLastXDays(7)->get();
关于php - 根据 laravel 中的 created_at 字段选择记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24475275/