我是 Laravel 和編碼的新手。試圖在表中顯示月份名稱(陣列)但不能這樣做。我嘗試了一些類似“json_encode”的東西,但每次都出錯。
我的控制器代碼是
$months = array();
for ($i = 0; $i < 12; $i ) {
$timestamp = mktime(0, 0, 0, date('n') - $i, 1);
$months[date('n', $timestamp)] = date('M-Y', $timestamp);
}
輸出是
array:12 [▼
3 => "Mar-2022"
2 => "Feb-2022"
1 => "Jan-2022"
12 => "Dec-2021"
11 => "Nov-2021"
10 => "Oct-2021"
9 => "Sep-2021"
8 => "Aug-2021"
7 => "Jul-2021"
6 => "Jun-2021"
5 => "May-2021"
4 => "Apr-2021"
]
查看檔案
<table id="incometable" class="table m-0 table table-bordered table-hover table" data-order='[[ 0, "desc" ]]'>
<thead>
<tr>
<th align="center">Month</th>
<th>Milk Sale Amount (Rs.)</th>
<th>Animal Sale Amount (Rs.)</th>
<th>Other Sale Amount (Rs.)</th>
</tr>
</thead>
<tbody>
@foreach ($months as $item )
<tr>
<td>{{ $item->$months }}</td>
</tr>
@endforeach
</tbody>
</table>
提前致謝
uj5u.com熱心網友回復:
在你的@foreach()回圈中,$item不是一個物件,它是一個'Mar-2022'通過'Apr-2021'. 您需要調整您的代碼:
@foreach($months as $key => $label)
<tr>
<td>{{ $label }}</td>
<tr>
@endforeach
如果您需要1通過12值,它們在每次迭代中都可以作為$key.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/439797.html
標籤:拉拉维尔
下一篇:在Laravel中確定url引數
