Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kdotsmith's avatar

Laravel: Undefined variable: posts (View: (frustrating)

So i have a problem that has been bugging me for a few days now. I want to display my posts on my homepage that is already displaying on my other pages. I think i have found why the information is not displaying and this is due to the Routes of the page. The view is looking fine and works correctly, however my controllers are the issue:

Web.php

	Route::get('/', 'PageController@index');

	Route::resource('postings', 'HomeController');

HomeController.php

public function __construct()
	{
			$this->middleware('auth');
	}


		    public function index()
				{
  				  $postings = Post::all();
    			  return view('Pages.welcome', compact('postings'));
				}

}

Welcome.blade.php

    @if(isset($postings) && count($postings) > 1)
			 @foreach($postings as $post)
   			 <h2><a href="/posts/{{$post->id}}">{{$post->title}}</a></h2>
		  @endforeach
    @else
    		</p>no posts found</p>
    @endif

The issue is in my WEB.PHP. PageController@index directs the page to the homepage, with HomeController being the controller that holds index. I then decided to create a function within that HomeController that allows me to display posts, however i keep getting error 'undefined error'. To conclude how would i insert a function in an existing controller that already has index.

0 likes
47 replies
Snapey's avatar

We had this a week ago?

Your web.php is using two controllers PageController and HomeController

Which is it you are trying to fix?

What do you mean by 'homepage'

kdotsmith's avatar

yes your back, so i kinda clocked the issue i was having which was the two controllers used for the one page. I am going to update my answer as i have partly solved the issue. I can get the undefined variable error to disappear but i now receive the error where it is showing no posts even though i have 4 posts in my database. I will update my answer now

Snapey's avatar

Were you updating your question?

From what I see.

You have a route / which goes to the PagesController (not shown)

You have a route /welcome which goes to the index1 method on the HomeController.

This controller gets the posts (as $postings)and renders the welcome view.

The welcome view iterates over the posts.

Thats it.

Pages controller is not relevant to the question

The / route is not relevant to the question.

So, when you visit /welcome you see the posts?

kdotsmith's avatar

The question has now updated. I removed the /welcome route as i'm now using the /postings route. Now when i get to the /welcome i see no posts, the message 'no posts found' appears which was hard coded in if there are no posts.

However i have 4 posts in my database but none are appearing.

Snapey's avatar

Now when i get to the /welcome i see no posts,

you will only see the posts when you go to the /postings route

kdotsmith's avatar

but postings is grabbing a resource right ? i have other routes like that of postings which follow the same kind of structure, why wont i see the posts and how could i potentially fix it ?

Snapey's avatar

You should see the posts when you go to /postings.

WHY would you see the posts when you go to /welcome

Snapey's avatar

Whats your point?

The route postings is named postings.index and routes to HomeController@index

kdotsmith's avatar

I have no page called postings, its just a variable within the index method that is in my HomeController. I have done the same for another page where i grab the variable posts using the route: Route::resource('posts', 'PostsController');

kdotsmith's avatar

but that should display my data postings right ?

Snapey's avatar

We have an expression 'the penny will drop in a moment'

MVC applications don't have 'pages' Although they appear to have.

They have a front-end router. This looks what you are asking for and directs you to a controller that can take care of it for you.

postings goes to HomeController. It can use a view called welcome.blade.php or anything.blade.php. There is ZERO relationship between the name of the view file and the route that you hit with your browser.

kdotsmith's avatar

so how will i exactly create that relationship between my view (welcome.blade.php) and route (HomeController.php). Are you saying get rid of the postings route and use something say /welcome ?

Snapey's avatar

what do you want to do?

Use concrete terms not like "show posts on my home page"

kdotsmith's avatar

I want to fetch the posts from my Database onto my view (Welcome.blade.php). My controller HomeController.php is set with an index function that fetches the data. However i cant get my posts to display on the page.

Now how would i set up a route for that ?

Snapey's avatar

ok, I'll just advise

HomeController

public function index()
{
    $postings = Post::all();
    return view('Pages.welcome', compact('postings'));
}

welcome.blade.php

@forelse($postings as $post)

        <h2><a href="/posts/{{$post->id}}">{{$post->title}}</a></h2>

@empty

        </p>no posts found</p>

@endforelse

Route

Route::get('/', 'HomeController@index')

or

Route::get('welcome', 'HomeController@index')

or

Route::get('postings', 'HomeController@index')

or

Route::get('anythingwhatever', 'HomeController@index')

keizah7's avatar

Just show us github repository and we will find your mistakes

kdotsmith's avatar

i still get the same issue as i did previously with the undefined variable postings when i use the recommended code

Snapey's avatar

are you sure you are editing resources/views/Pages/welcome.blade.php

kdotsmith's avatar

100% yes i am. is there a way you can see my whole project, i guess if i load it to github you may get a better understanding of the code right ?

jlrdw's avatar

I would recommend watching some more from scratch videos, Jeffrey covers basic crud.

You just need to set up a correct route to load the correct view.

jrdavidson's avatar

@kdotsmith Why do you have all of this duplication at the bottom of your routes/ web.php file.

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
kdotsmith's avatar

okay, i did not seee that, i think that may have been done accidentally today, i will remove it now

1 like
Snapey's avatar

still, the same issues.

Route::get('/', 'PageController@index');

page controller


class PageController extends Controller
{
    public function index() {
        return view('pages.welcome');
    }

So the route / goes to page controller, which returns the welcome view, but does not initialise the $postings variable.

meanwhile /postings probably works fine.

Also, be MORE careful with the names for things. On some operating systems resources/views/Pages is not the same as resources/views/pages but you seem to use the two without much care

Next

Please or to participate in this conversation.