![](/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.