1

So, I have this array of arrays, something like this:

protected static $base = [
        [
            'id' => 4,
            'createdAt' => 1498638933,
            'title' => 'Example 1',
            'tables' => [
                ['title' => '#1', 'createdAt' =>  1498638940],
                ['title' => '#2', 'createdAt' =>  1498638941],
                ['title' => '#3', 'createdAt' =>  1498638943],
            ],
        ],
        [
            'id' => 7,
            'createdAt' => 1498643923,
            'title' => 'Example 2',
            'tables' => [
                ['title' => '#1',  'createdAt' =>  1498643925],
                ['title' => '#2',  'createdAt' =>  1498643929],
                ['title' => '#3',  'createdAt' =>  1498643932],
            ],

these arrays are in a Model code, and I want to display them in my page. Also, from the Model:

    public static function getAll() {
    sleep(1);
    return self::$base;
}

From web.php (routes), I have:

Route::get('/', function () {
    $items = \App\MyModel::getAll();
    return view('welcome')->with('items', $items);
});

Now, if I try:

       {!! dd($items) !!}

All the arrays are displayed, but not in a pretty way. I want only their content. So, I followed these steps (https://laracasts.com/discuss/channels/general-discussion/passing-arrays-to-blade-and-iterating) which became:

@foreach($items['title'] as $title => $content)
    {{ $title }}
    @foreach ($content as $tables => $item)
        {{ $tables . ' => ' . $item }}
    @endforeach
@endforeach

But 'title' isn't recognized, and I've tried some things like this, nothing works. What I might be missing?

5
  • Shouldnt it be @foreach($items as $item) {{ $item['title'] }} @endforeach Commented Jul 10, 2017 at 23:48
  • It worked for the first loop... what about the arrays inside of this array? Commented Jul 10, 2017 at 23:51
  • Simply update to include those inside the first loop too - @foreach($item['tables'] as $table) {{ $table['title'] }} @endforeach Commented Jul 10, 2017 at 23:52
  • Thank you so much!! It worked! Commented Jul 10, 2017 at 23:55
  • No problem. Ive added it as an answer so you can upvote and accept which will close the Question on SO Commented Jul 10, 2017 at 23:56

2 Answers 2

1

You are trying to loop over an array key inside an array of arrays which is not possible.

You first need to loop through the outer arrays and then you can access the content of the keys inside such as below:

@foreach($items as $item)
   {{ $item['title'] }}
   @foreach($item['tables'] as $table)
      {{ $table['title'] }}
   @endforeach
@endforeach
Sign up to request clarification or add additional context in comments.

Comments

1

First of all, if you return variables with a view, you can better use php's compact function so you won't repeat yourself. Here's an example:

return view('welcome', compact('items'));

And since you use dd() it dumps everything in the array and then dies. If you want to show all items you should indeed use a foreach loop, but like this:

@foreach($items as $item)
   {{ $item['title'] }}
   @foreach($item['tables'] as $table)
      {{ $table['title'] }}
   @endforeach
@endforeach

Comments

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.