>我有 4 个radio buttons
,每个对应于一个时间段:
- 上个星期
- 上个月
- 最后一天
我想根据这些时间选择数据库记录。
例如,当用户选择上个月时,应显示上个月到现在的所有记录,但我的created_at field
不起作用。
你的想法是什么?
在您的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();