我可以在Laravel5上获取摘录(列表)的自定义日期格式吗?
问题描述:
我的数据库记录是这个.
My DB records are this.
tests table
id date
1 2016-07-01
2 2016-07-31
3 2016-08-01
4 2016-08-15
5 2016-08-31
6 2016-09-01
我想按月选择记录. 现在我的代码就是这个.
I wanna choose record by month. Now my code is this.
控制器
$months = \App\Test::where('date', '<=', 'now()')
->orderBy('date', 'desc')
->pluck('date', 'date');
查看
{{ Form::select('month', $months, old('month'), ['id' => 'month']) }}
( that generate this. )
<select id="month" name="month">
<option value="2016-07-01">2016-07-01</option>
<option value="2016-07-31">2016-07-31</option>
<option value="2016-08-01">2016-08-01</option>
<option value="2016-08-15">2016-08-15</option>
</select>
但是我想要这个.
$months = \App\Test::where('date', '<=', 'now()')
->orderBy('date', 'desc')
// ->format('m').
->pluck('date', 'date');
{{ Form::select('month', $months, old('month'), ['id' => 'month']) }}
( I wanna generate this )
<select id="month" name="month">
<option value="7">7</option>
<option value="8">8</option>
</select>
我想使用format,但可悲的是无法做到这一点 有解决办法吗?
I wanna use format with pluck but cant do this sadly Any solves?
答
谢谢大家,我昨天放弃了从DB取车的月份, 但今天!!使用此代码XD,我得到了正确答案. 注意,此代码仅适用于Postgresql, 而且... now()也在哪里.这是postgres代码. Mysql或sqlite必须更改数据库原始数据以及代码在何处.
Thanks Everyone, I had given up to pickup month from DB yesterday, But Today!! I got TRUE Answer with this code XD. NOTE, This code will work Postgresql only, And where ... now() is too. this is postgres code. Mysql or sqlite have to change DB raw and where code ya.
$months = \App\Test::select(\DB::raw('DATE_PART(\'MONTH\', date) AS MONTH'))
->where('date', '<=', 'now()')
->orderBy('date', 'desc')
->pluck('month', 'month');