Support the ongoing development of Laravel.io →
Authentication Session Database

I'm trying to output my posts and for that in a specific page I call a function with foreach to load all the posts, and the function in my controller is something like:

public function getDashboard() { $posts = Post::all(); return view('dashboard', ['posts' => $posts]); }

and my route is something like this:

Route::get('/dashboard',[ 'uses' => 'PostController@getDashboard', 'as' => 'dashboard'

]);

but get an error when I try to load the page when i'm not logged in the error is something like: Undefined variable: posts (View: C:\wamp64\www\nokinst\resources\views\dashboard.blade.php)

Anything you say is highly appreciated.

(I use Laravel 5.3.26 )

Last updated 3 years ago.
0

You have to write your controller method like this way using "compact"

public function getDashboard() { 
  $posts = Post::all(); 
  return view('dashboard', compact('posts')); 
}
0

I actually tried that and it didnt work

0

Sign in to participate in this thread!

PHPverse

Your banner here too?

nikona14 nikona14 Joined 8 Dec 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.