如何在Laravel 5.2中包含刀片文件

时间:2017-09-09 06:56:28

标签: php laravel-5

我正在开发Laravel应用程序,我需要在每个项目的每个任务文件中添加注释表单。 这是comments / 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>

我打算将此表单文件包含在视图文件的tasks文件夹中的show.blade.php文件中。 这是show.blade.php

<h2>{{ $tasks->project->project_name }}</h2>
<hr>

{{$tasks->task_name}}
<hr>

{!!$tasks->body!!}

<hr>

@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');
    }

routes.php文件

Route::post('projects/{projects}/comments', [
    'uses' => 'CommentsController@postNewComment',
    'as'   => 'projects.comments.create',
    'middleware' => ['auth']
]);

但终于得到了这个错误按摩

Undefined variable: project (View: C:\Users\Nalaka\Desktop\acxian\resources\views\comments\form.blade.php)

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您尚未在任何地方定义$project,但您已$tasks已从show.blade.php获取项目名称,因此,如果您在$tasks->project数据中也有项目ID那么您可以在 comments / form.blade.php 中的视图更改form标记中使用此变量,如下所示:

<form class="form-vertical" role="form" method="post" action="{{ route('projects.comments.create', $tasks->project->id) }}">