更新可变刀片模板

时间:2019-02-07 13:05:24

标签: javascript php laravel variables laravel-blade

我正在尝试更新sprint的变量方面,以用所选sprint的任务填充表格。

这是用于选择sprint的标签:

@foreach($project->sprints as $s)
    <a href="#" >{{$s->version}}</a>
@endforeach

接下来,任务表:

@php ($sprint = null)
@if($sprint != null)
   <table class="table table-striped">
       <thead>
            <tr>
                ...
            </tr>
       </thead>
       <tbody>
            @foreach($sprints->tasks as $task)
               <tr>
                   ...
               </tr>
            @endforeach
       </tbody>            
   </table>
@else
...

如何将$ s分配给$ sprint?

1 个答案:

答案 0 :(得分:1)

您必须处理控制器中的选定冲刺。

假设您已定义:

  • 路线

    Route::get('/project/{projectId}', [
        'uses' => 'ProjectController@show',
        'as' => 'showProject'
    ])
    
  • 控制器

    namespace App\Http\Controllers;
    
    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    use App\Project;
    
    class ProjectControllerextends Controller
    {
        public function show(int $projectId, Request $request)
        {
            $project = Project::with(['sprints.tasks'])->find($projectId);
    
            $selectedSprint = $request->query('selectedSprint')
    
            if (!empty($selectedSprint)) {
                $selectedSprint = $project->sprints->first(function($sprint) use ($selectedSprint) {
                    return $sprint->id === (int) $selectedSprint;
                });
            }
    
            return view('projects.show', ['project' => $project, 'selectedSprint' => $selectedSprint]);
        }
    }
    
  • 查看

    @foreach($project->sprints as $s)
        <a href="{{route('showProject', ['projectId' => $project->id, 'selectedSprint' => $s->id])}}">
            {{$s->version}}
        </a>
    @endforeach
    
    @if($selectedSprint != null)
       <table class="table table-striped">
           <thead>
                <tr>
                    ...
                </tr>
           </thead>
           <tbody>
                @foreach($selectedSprint->tasks as $task)
                   <tr>
                       ...
                   </tr>
                @endforeach
           </tbody>            
       </table>
    @else