-1
return view($this->folder_path . 'index', compact('data' ));

I want to use foreach to display data.

array:5 [▼
  0 => array:4 [▶]
  1 => array:4 [▼
    "id" => 2
    "name" => "test"
    "desc" => "my name"
    "age" => 12
  ]

This is view where i want to display data

@foreach($data as $item)
 <tr>
  <td>{{ $loop->iteration }}</td>
  <td>{{ ucwords($item->name) }}</td>
  <td>{{ $item->desc }}</td>
  <td>{{ $item->age }}</td>
 <tr>
@endforeach
5
  • 2
    laravel.com/docs/9.x/blade#loops Commented Oct 13, 2022 at 10:54
  • 1
    Please don't post code in the comments, hard to read. Edit your question and paste the code there. Thanks Commented Oct 13, 2022 at 11:08
  • What does "not working" mean? Blank page? No data shown? Wrong data shown? Any error? Commented Oct 13, 2022 at 11:09
  • Shot in the dark: iirc data/$data has a "special meaning" in Laravel when passing data to views. Try using a different name for data. Also maybe {{ dd($data) }} in your view to see what it contains Commented Oct 13, 2022 at 11:19
  • It has the array of data Commented Oct 13, 2022 at 11:22

1 Answer 1

1

Your $data and each $item appear to be arrays and not objects, therefore using the object accessor (->) on $item will not work. Instead you should be accessing the elements of your $item array using their key names:

@foreach($data as $item)
 <tr>
  <td>{{ $loop->iteration }}</td>
  <td>{{ ucwords($item['name']) }}</td>
  <td>{{ $item['desc'] }}</td>
  <td>{{ $item['age'] }}</td>
 <tr>
@endforeach

Probably worthwhile doing some reading on associative arrays.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.