I have tried this and it does not work
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.
Please or to participate in this conversation.