I am newbie for laravel and doing my first project called blog. For post article i can fetch data but I am trying to display reader's comment in the index page just below the article by fetching from database but it gives error(For info,I have already inserted a row from xammp
just to fetch).Here is the code for PostController.php
public function index()
{
//$show = Post::all();
$show = Post::orderBy('id','desc')->paginate(1);
return view('pages/blog')->with('post',$show);
}
public function comment()
{
$show = readerComment::all();
return view('pages/blog')->with('commentShow',$show);
}
In index pages or blog.blade.php
@if(count($post)>0)
@foreach($post as $article)
<div class = "row">
<div class="col-md-12">
<h3 class="text-center">{{$article->title}}</h2>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p>{!!$article->article!!}</p>
</div>
</div>
<!-- Comment section -->
<div class = "comment">
<h3>Comments</h3>
@foreach($commentShow as $commShow)
<div class = "row">
<div class = "col-md-12">
<p>{{$commShow->comment}}</p>
<p>{{$commShow->name}}</p>
</div>
</div>
@endforeach
</div>
And in web route
Route::resource('posts','PostController');
Route::get('/','PostController@comment');
I get error as
Undefined variable: post (View: C:\xampp\htdocs\bloggingesources\views\pages\blog.blade.php)
Any help would be appreciated. Thanks
Do it like that,
public function index()
{
$posts = Post::orderBy('id','desc')->paginate(1);
return view('pages.blog',compact('posts'));
}
You're blade looks good without error, but let's try this view..
@extends('layouts.app')
@section('content')
@if(count($post)>0)
@foreach($post as $article)
<div class = "row">
<div class="col-md-12">
<h3 class="text-center">{{$article->title}}</h2>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p>{!!$article->article!!}</p>
</div>
</div>
<!-- Comment section -->
<div class = "comment">
<h3>Comments</h3>
@foreach($post as $commShow)
<div class = "row">
<div class = "col-md-12">
<p>{{$commShow->comment}}</p>
<p>{{$commShow->name}}</p>
</div>
</div>
@endforeach
</div>@endsection
in addition to this:
When you use the
->with()
method the first parameter passed to it is the name of the variable available on the view. So for the comment part you make this call->with('commentShow',$show);
yet in your view you try and access it via$post
. Change this line@foreach($post as $commShow)
to@foreach($commentShow as $commShow)
change you view code to this: (please note the changed variable name in second @foreach
)
@if(isset($post) && !empty($post))
@foreach($post as $article)
<div class="row">
<div class="col-md-12">
<h3 class="text-center">{{$article->title}}</h2>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p>{!!$article->article!!}</p>
</div>
</div>
@endforeach
@endif
@if(isset($commentShow) && !empty($commentShow))
<!-- Comment section -->
<div class="comment">
<h3>Comments</h3>
@foreach($commentShow as $commShow)
<div class="row">
<div class="col-md-12">
<p>{{$commShow->comment}}</p>
<p>{{$commShow->name}}</p>
</div>
</div>
@endforeach
</div>
@endif
Here you are defining your Eloquent Collection of models to be $commentShow
:
return view('pages/blog')->with('commentShow',$show);
In your view, you are using the variable $post
. You need to change one of them to match the other.