Reputation: 55
I am trying to create a blade view so that I can see the comments from a particular post in my app, but I am getting an undefined variable error:
ErrorException in a11883d0293a033eba2997ec69df8cdf4ea6a835.php line 9: Undefined variable: comment (View: C:\xampp\htdocs\directory\resources\views\admin\comments\show.blade.php)
I am new to learning Laravel and apologize if this has been answered before but I have been unable to resolve the issue through research on this site thus far. Thank you!
show.blade.php
@extends('layouts.admin')
@section('content')
@if($comment)
<h1>Comments</h1>
<table class="table">
<thead>
<tr>
<th>id</th>
<th>Author</th>
<th>Email</th>
<th>Body</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{$comment->id}}</td>
<td>{{$comment->author}}</td>
<td>{{$comment->email}}</td>
<td>{{$comment->body}}</td>
<td><a href="{{route('home.post', $comment->post->id)}}">View Post</a></td>
<td>
@if($comment->is_active === 1)
{!! Form::open(['method'=>'PATCH','action'=> ['PostCommentsController@update', $comment->id]]) !!}
<input type="hidden" name="is_active" value="0">
<div class="form-group">
{!! Form::submit('Un-approve', ['class'=>'btn btn-info']) !!}
</div>
{!! Form::close() !!}
@else
{!! Form::open(['method'=>'PATCH','action'=> ['PostCommentsController@update', $comment->id]]) !!}
<input type="hidden" name="is_active" value="1">
<div class="form-group">
{!! Form::submit('Approve', ['class'=>'btn btn-success']) !!}
</div>
{!! Form::close() !!}
@endif
</td>
<td>
{!! Form::open(['method'=>'DELETE','action'=> ['PostCommentsController@destroy', $comment->id]]) !!}
<div class="form-group">
{!! Form::submit('Delete', ['class'=>'btn btn-danger']) !!}
</div>
{!! Form::close() !!}
</td>
</tr>
</tbody>
</table>
@else
<h1 class="text-center">No Comments</h1>
@endif
@endsection
PostCommentsController.php
namespace App\Http\Controllers;
use App\Comment;
use App\Post;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Auth;
class PostCommentsController extends Controller
{
public function index()
{
$comments = Comment::all();
return view('admin.comments.index', compact('comments'));
}
public function store(Request $request)
{
$user = Auth::user();
$data = [
'post_id' => $request->post_id,
'author' => $user->name,
'email' => $user->email,
'photo' => $user->photo->file,
'body' => $request->body
];
Comment::create($data);
$request->session()->flash('comment_message', 'Your message has been submitted and is waiting moderation');
return redirect()->back();
}
public function show($id)
{
$post = Post::findOrFail($id);
$comments = $post->comments;
return view('admin.comments.show', compact('comments'));
}
public function update(Request $request, $id)
{
Comment::findOrFail($id)->update($request->all());
return redirect('/admin/comments');
}
public function destroy($id)
{
Comment::findOrFail($id)->delete();
return redirect()->back();
}
}
Comment.php Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $fillable = [
'post_id',
'author',
'email',
'body',
'is_active'
];
public function replies()
{
return $this->hasMany('App\CommentReply');
}
public function post()
{
return $this->belongsTo('App\Post');
}
}
Upvotes: 0
Views: 1044
Reputation: 14268
What you are missing is iterating over the $comments
that you have passed to the view.
So try replacing your <tbody>
content with the following:
@foreach($comments as $comment)
<tr>
<td>{{$comment->id}}</td>
<td>{{$comment->author}}</td>
<td>{{$comment->email}}</td>
<td>{{$comment->body}}</td>
<td><a href="{{route('home.post', $comment->post->id)}}">View Post</a></td>
<td>
@if($comment->is_active == 1)
{!! Form::open(['method'=>'PATCH','action'=> ['PostCommentsController@update', $comment->id]]) !!}
<input type="hidden" name="is_active" value="0">
<div class="form-group">
{!! Form::submit('Un-approve', ['class'=>'btn btn-info']) !!}
</div>
{!! Form::close() !!}
@else
{!! Form::open(['method'=>'PATCH','action'=> ['PostCommentsController@update', $comment->id]]) !!}
<input type="hidden" name="is_active" value="1">
<div class="form-group">
{!! Form::submit('Approve', ['class'=>'btn btn-success']) !!}
</div>
{!! Form::close() !!}
@endif
</td>
<td>
{!! Form::open(['method'=>'DELETE','action'=> ['PostCommentsController@destroy', $comment->id]]) !!}
<div class="form-group">
{!! Form::submit('Delete', ['class'=>'btn btn-danger']) !!}
</div>
{!! Form::close() !!}
</td>
</tr>
@endforeach
And at the top, replace the @if
with
@if($comments->count())
Upvotes: 1