-1

Inside Route, I am trying to pass an array inside view method and trying to display all the data in my blade template which that view is returning. While hitting that route, i am getting this error.

This is web.php file with all the routes

Route::get('/listing', function(){
    return view('listings', [
        'heading' => 'Latest Listing',
        'listings' => [
            'id' => 1,
            'title' => 'Listing One',
            'description' => 'Description One for Listing one... also some random text to make it look like a description.'
        ],
        [
            'id' => 2,
            'title' => 'Listing Two',
            'description' => 'Description Two for Listing two... also some random text to make it look like a description.'
        ]
    ]);
});

**This is listings.blade.php file **

<h1>{{$heading}}</h1>
@foreach ($listings as $listing)
    <h2>{{$listing['title']}}</h2>
@endforeach
Saani Info
  • 67
  • 6
  • What is the full error? (Message/file/line) – Luke Snowden Jun 20 '23 at 13:41
  • 5
    You do not have an array of arrays there, you have one array assigned to `listings`, and then that's followed by another array, that has no reference to it at all. The first array element you encounter in your loop is `'id' => 1`, and `1` is of course an integer. You need an extra level of `[...]` in your route's return value there: `'listings' => [ [...], [...] ]` – CBroe Jun 20 '23 at 13:42
  • Does this answer your question? [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Nico Haase Jun 20 '23 at 14:12

1 Answers1

5

You did a mistake while creating array of listings, replace your code with below code.

Route::get('/listing', function(){
return view('home', [
    'heading' => 'Latest Listing',
    'listings' => [[
        'id' => 1,
        'title' => 'Listing One',
        'description' => 'Description One for Listing one... also some random text to make it look like a description.'
    ],
    [
        'id' => 2,
        'title' => 'Listing Two',
        'description' => 'Description Two for Listing two... also some random text to make it look like a description.'
    ]]
]);
});
Hercules73
  • 181
  • 4