我需要在My Laravel应用中添加评论框。这是视图文件的评论文件夹中的评论框的刀片文件 评论/ form.blade.php
<form class="form-vertical" role="form" method="post" action="{{ route('projects.comments.create', $project->id) }}">
<div class="form-group{{ $errors->has('comments') ? ' has-error' : '' }}">
<textarea name="comments" class="form-control" style="width:80%;" id="comment" rows="5" cols="5"></textarea>
@if ($errors->has('comments'))
<span class="help-block">{{ $errors->first('comments') }}</span>
@endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Add Comment</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
</div>
现在我要将此文件包含在tasks文件夹中的show.blade.php文件中 任务/ show.blade.php
@include('comments.form')
这是我的CommentController.php
public function postNewComment(Request $request, $id, Comment $comment)
{
$this->validate($request, [
'comments' => 'required|min:5',
]);
$comment->comments = $request->input('comments');
$comment->project_id = $id;
$comment->user_id = Auth::user()->id;
$comment->save();
return redirect()->back()->with('info', 'Comment posted successfully');
}
和评论模型
public function user()
{
return $this->belongsTo(User::class);
}
但出现以下错误
Undefined variable: project (View: C:\Users\Lilan\Desktop\ratakaju\resources\views\comments\form.blade.php)
如何解决这个问题?