如何将刀片文件包含到Laravel中的另一个刀片文件中

时间:2016-08-12 17:55:56

标签: php laravel-5

我需要在项目文件夹中带有index.blade.php的协作者文件夹中的@include My form.blade.php文件。

这是index.blade.php文件

@extends('layouts.app')
@section('content')

<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
@include('layouts.partials.alerts')
<h1 class="page-header">Projects<a class="btn btn-info" href="{{ route('projects.create') }}">New Project</a></h1>

<div class="container">
  @if($project)

     <div class="row">
         @foreach ($project as $proj)
           <div class="col-md-3" style="border:1px solid #ccc;margin-left:5px;">
           <h2><a href="/projects/{{ $proj->id }}">{!! $proj->project_name !!}</a></h2>
           <p>Due : {!! date_format(new DateTime($proj->due_date), "D, m Y") !!}</p>
           <p>Status: {!! $proj->project_status !!}</p>
           <p>Tasks: 0</p>
           <p>Comments: 0</p>
           <p>Attachments: 0</p>
           </div>

        @endforeach
     </div>
  @endif

  <div>
  @include('collaborators.form')<!-- This is include file -->
  </div>

  @if($project->isEmpty())
     <h3>There are currently no Projects</h3>
  @endif 
</div>





<div class="container">
   <a class="btn btn-info" href="{{ route('projects.create') }}">New Project</a>

</div>

这是我的form.blade.php文件

 <div class="col-md-4" style="border:1px solid #ccc;margin-left:15px;padding:10px;">
    <h4 class="page-header">
        Collaborators
    </h4>
    @if($collaborators)
       @foreach( $collaborators as $collaborator)
            <div>
                <div>
                    <span>
                        <img src="{{ $collaborator->user()->first()->getAvatarUrl() }}" />
                         {{ $collaborator->user()->first()->username}}
                         {{ $collaborator->user()->first()->id}}

                    </span>
                </div>
                <button class="btn btn-sm btn-danger delete" style="margin-top:5px;padding:4px;width:35px;"
                  data-action="/projects/{{ $project->id }}/collaborators/{{ $collaborator->collaborator_id }}"
                  data-token="{{csrf_token()}}">
                <i class="fa fa-trash-o"></i>
                </button>

                <!-- permission start -->

                <form class="form-vertical" role="form" method="post" action="{{ route('projects.collaborators.permission', $project->id) }}">
                    <!--<div id="cid" name="cid">{{ $collaborator->user()->first()->id}}</div>-->
                <input type="number" id="cid" name="cid" value="{{ $collaborator->user()->first()->id }}" />
                <div class="form-group{{ $errors->has('status') ? ' has-error' : '' }}">
                <label for="status" class="control-label">Choose Permission</label>
                <select name="status" id="status">
                <option value="">Choose a status</option>
                <option value="3">View Only</option>
                <option value="2">Edit Tasks</option>
                <option value="1">Admin</option>
                </select>
                @if ($errors->has('status'))
                <span class="help-block">{{ $errors->first('status') }}</span>
                @endif
                </div>
                  <div class="form-group">
                    <button type="submit" class="btn btn-default">Create</button>
                  </div>
                  <input type="hidden" name="_token" value="{{ csrf_token() }}"> 


               </form>

                <!-- permission end -->


            </div>
            <hr/>
       @endforeach
    @endif
    <form class="form-vertical" role="form" method="post" action="{{ route('projects.collaborators.create', $project->id) }}">
    <div class="form-group{{ $errors->has('collaborator') ? ' has-error' : '' }}">
        <label> Add New </label>
        {!! mention()->asText('collaborator', old('collaborator'), 'users', 'username', 'form-control') !!}
        @if ($errors->has('collaborator'))
            <span class="help-block">{{ $errors->first('collaborator') }}</span>
        @endif
    </div>

    <div class="form-group">
        <button type="submit" class="btn btn-info">Add User</button>
    </div>
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    </form>

但是在包含文件后生成以下错误

Undefined variable: collaborators (View: C:\Users\Fernmax\Desktop\c\resources\views\collaborators\form.blade.php)

可以给我解决方案吗?

感谢!!!

2 个答案:

答案 0 :(得分:0)

通过collaborators查看:

@include('collaborators.form', ['collaborators' => $yourData])

答案 1 :(得分:0)

在设置$collaborators变量后,将$collaborators从控制器传递到您的视图或路由回调,例如:

return view('index')->with('collaborators', $collaborator);

您的$collaborators变量应自动传递到包含的collaborators.form视图