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\projectnameesources\views\dashboard.blade.php)
Anything you say is highly appreciated.
(I use Laravel 5.3.26 )
Here you go this is my blade, but I don't think this is where the problem is:
@extends('layouts.master')
@section('content')
@foreach($posts as $post)
<article class="post">
<div class="media w3-border-left w3-border-blue">
<div class="media-left media-top">
</div>
<div class="media-body">
<header><h4 class="media-heading">{{ $post->title }}</h4></header>
<p>{{ $post->body }}</p>
<div class="info">
Posted by {{ $post->user->name }} on {{ $post->created_at }}
</div>
<div class="interaction">
<a href="#" class="like">Like</a> |
<a href="#" class="like">Dislike</a> |
<a href="#" class="like">Edit</a> |
<a href="#" class="like">Delete</a>
</div>
</div>
</div>
<br>
</article>
@endforeach
@endsection