简体   繁体   中英

how to include blade file in Laravel 5.2

I am developing Laravel application and I need add comment form to My each task file regarding to each project. this is 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>

I am going to include this form file to show.blade.php file in tasks folder in view file. this is 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']
]);

but finally got this error massage

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

how can fix this problem?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM