0

Could someone let me know why I get this error:

"Undefined variable: listings (View: \app\views\pages\listings.blade.php)"

Controller

class ListingsController extends BaseController {

    public function showListings() {
        $listings = Listing::paginate(10);
        return View::make('pages.listings', $listings);
    }

}

View

    <div class="container">

        <div id="main">
            <div class="row">
                <div class="span9">
                    <h1 class="page-header">Listings</h1>
                        @foreach($listings as $listing)
                            {{ $listing->address }}
                        @endforeach
                </div>
            </div>
        </div>
    </div>
    @include('includes.footer')

1 Answer 1

4

Try doing like this:

return View::make('pages.listings')->with('listings', $listings);

or

return View::make('pages.listings', array('listings' => $listings));

or even

return View::make('pages.listings', compact('listings'));
Sign up to request clarification or add additional context in comments.

3 Comments

Or return View::make('pages.listings', compact('listings')); Or with PHP 5.4 return View::make('pages.listings', ['listings' => $listings]);. It's a little cleaner.
Thanks! I remember seeing it with the "with" statement in the Laravel Quick Start but the one I was following had it like in my question.
Every time you get stuck with something, it's better to take a look in the specific docs page for the command you're using: laravel.com/docs/responses. The basic stuff is usually there and it helps a lot.

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.