我已经在db“2014-10-13”中存储了这种格式的日期,我需要在Laravel中显示给定月份的记录。
答案 0 :(得分:0)
使用mysql Month函数
SELECT * FROM tbl_name WHERE MONTH( col_name ) = 3
在laravel:
$name = DB::table('tbl_name')->whereRaw('MONTH(col_name) = 3')->get();
答案 1 :(得分:0)
试试这个会起作用:
SELECT * FROM table_name WHERE MONTH('2014-10-13') = '10';
幼虫:
$results = DB::select('select * from users where MONTH('2014-10-13') = ?', array(10));
答案 2 :(得分:0)
让我假设表名为用户,并假设您使用了eloquent,因为您已经在laravel下标记了它。
这是您获取在特定月份创建的用户的方式。
雄辩:
$month = 12;// for december
$users = User::whereRaw('MONTH(created_at) = ?',[$month])->get();
return $users;
查询构建器:
$users = DB::table('users')->whereRaw('MONTH(created_at) = ?',[$month])->get();
如果您还想在结果中获得月份
雄辩:
$users = User::select(['*',DB::raw('MONTH(created_at) as month')])
->whereRaw('MONTH(created_at) = ?',[$month])->get();
查询构建器:
$users = DB::table('users')->select(['*',DB::raw('MONTH(created_at) as month')])
->whereRaw('MONTH(created_at) = ?',[$month])->get();