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 )
You have to write your controller method like this way using "compact"
public function getDashboard() {
$posts = Post::all();
return view('dashboard', compact('posts'));
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community