![](/img/trans.png)
[英]Can I get custom date format for pluck(lists) on Laravel5?
[英]How to add strings to array key if that made by laravel5 pluck(lists) method?
昨天我解决了这个问题 ,谢谢大家。
下一个问题。 不是那么重要,但我感到担心,看看这段代码。
调节器
$months = \App\Test::select(\DB::raw('DATE_PART(\'MONTH\', date) AS MONTH'))
->where('date', '<=', 'now()')
->orderBy('date', 'desc')
->pluck('month', 'month');
// this code generate like this.
// Illuminate\Support\Collection Object ( [items:protected] => Array ( [8] => 8 [7] => 7 ) )
视图
{{ Form::select('month', $months, old('month'), ['id' => 'month']) }}
( now generate this. )
<select id="month" name="month">
<option value="8">8</option>
<option value="7">7</option>
</select>
我希望像这样添加字符串到键
<select id="month" name="month">
<option value="8">8month</option>
<option value="7">7month</option>
</select>
我认为这可以像foreach这样做。
$array = ["8" => "8", "7" => "7"];
print_r($array); // Array ( [8] => 8 [7] => 7 )
foreach($array as $key => $value){
$array[$key.'month'] = $value;
unset($array[$key]);
}
print_r($array); // well done! Array ( [8month] => 8 [7month] => 7 )
所以测试一下,但......
print_r($months); // Illuminate\Support\Collection Object ( [items:protected] => Array ( [8] => 8 [7] => 7 ) )
foreach($months as $key => $value){
$array[$key.'month'] = $value;
unset($months[$array]);
}
print_r($months); // Not Working WTF!! Illuminate\Support\Collection Object ( [items:protected] => Array ( ) )
任何解决?
您的$months
变量是Collection
实例。 您可以使用$months->put($key, $value)
或$months->push($value)
在这里查看colllection方法
编辑:
另外,我注意到你在第二个例子中使用了错误的变量。 不应该这样吗?
foreach($months as $key => $value){
$months[$key.'month'] = $value;
unset($months[$key]);
}
那个错误就是答案lmao XD真正做到这一点,原谅我的愚蠢。
foreach($months as $key => $value){
$months[$key.'month'] = $value;
unset($months[$key]);
}
PS
以上代码是MISTAKE
本准则是真实的。
foreach($months as $key => $value){
$months[$value] = $value.'month';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.