1

I am new to Laravel and coding. Trying to show Month name (Array) in table but not able to do the same. I tried few things like "json_encode" but got error each time.

My controller code is

 $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);
                    }

Output is

     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"
              ]

View file

    <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>

Thanks in Advance

0

1 Answer 1

2

During your @foreach() loop, $item is not an Object, it is a String 'Mar-2022' through 'Apr-2021'. You need to adjust your code:

@foreach($months as $key => $label)
  <tr>
    <td>{{ $label }}</td>
  <tr>
@endforeach

If you need the 1 through 12 values, they are available in each iteration as $key.

Sign up to request clarification or add additional context in comments.

3 Comments

just one more point. If i need to add another <td> .... </td> Say total income amount , how can i add the same....
With your current code, that won't work; your array is key => string, like 1 => 'Jan-2022'. You'd need to modify it to be key => array or key => object, like 1 => ['label' => 'Jan-2022', 'income' => 100] then you'd be able to add an additional <td> element.
Attempt to add that yourself, and if you get stuck, please ask a new question 🙂

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.