代码之家  ›  专栏  ›  技术社区  ›  Jacobo

Laravel:缺少必需的参数

  •  0
  • Jacobo  · 技术社区  · 6 年前

    我弄错了 missing required parameters ,路线如下: Route::get('/myunits/{group}/content/{unit}','Users\GroupContentController@show')->name('user.group.unit.show');

    当我们重定向到该路由时,路由是正确的,但由于某种原因它失败了。但当我对上的参数执行dd()时 GroupContentController@show 参数在那里,所以我不知道错误在哪里。

    这是我的控制器

    public function show(Group $group , Unit $unit) {
    
    
        /*
        * Check if the user is trying to acces a group
        * here he does not belongs.
        */
        if ( !UserGroupFacade::IsStudentInGroup($group) ) {
            return redirect()->route('user.groups');
        }
    
        $data = [];
        $groupMaterials = $group->groupMaterials->where("unit_id" , $unit->id);
    
        foreach ($groupMaterials as $gm) {
            foreach ($unit->themes as $theme) {
                if ($theme->id == $gm->theme_id) {
                    $theme->show=true;
                    $material=$theme->materials->where("id" , $gm->material_id)->first();
                    $material->show=true;
                }
            }
        }
    
        $data['unit'] = $unit;
        return view('users.units.show', array('data' => $data));
    }
    

    当我做了一个dd($group)和dd($unit)的时候,所有的东西都在那里,甚至在返回之前我也试过了,但它仍然有效。

    应显示的HTML如下:

    @extends('layouts.main')
    
    @section('title')
      Unit
    @endsection
    
    @php
      $unit = $data['unit'];
    @endphp
    
    @section('content')
      <section class="goal-app-header-section">
        <div class="container">
          <div class="image-div float-content left thirty">
            <img style="width: 182px; height: 182px; object-fit: contain; border-radius: 50%;" src="{{ $unit->image }}" alt="Unidad">
          </div>
          <div class="description float-content right seventy">
            <h1> {{ $unit->name }} </h1>
            <p class="description"> {!! $unit->description !!} </p>
          </div>
        </div>
      </section>
    
      <section class="bars-section">
        <div>
          <div class="text-center bars blue float-content left half">
            <div class="row"><h2> Themes </h2></div>
          </div>
          <div class="text-center bars green float-content right half">
            <div class="row"><h2> Media tools </h2></div>
          </div>
        </div>
      </section>
    
    
      <section class="content-section">
    
        <input id="view-name" type="hidden" value="single-unit-view">
        <div>
          <div class="float-content left half">
    
            <?php $number = 0; ?>
            @foreach ($unit->themes as $index => $theme)
              @if ($theme->show)
              <?php $number++; ?>
              <div id="{{ $number }}" class="leftArrow inactive">
                <div class="row">
                  <div class="col-xs-12 col-xs-offset-0 col-sm-12 col-sm-offset-0 col-md-1 col-md-offset-1 col-lg-1 col-lg-offset-1">
                    <div class="numberCircle"><div class="number">{{ $number }}</div></div>
                  </div>
                  <div class="col-xs-12 col-sm-12 col-md-10 col-lg-10">
                    <p class="left">
                      {{ ($theme->title) }}
                    </p>
                  </div>
                </div>
              </div>
              @endif
            @endforeach
    
          </div>
    
          <div class="content-list float-content left half">
            <?php $number2 = 0; ?>
            @foreach ($unit->themes as $index => $theme)
              @if ($theme->show)
              <?php $number2++; ?>
              <div id="course-content-{{ $number2 }}" class="col-xs-12 col-sm-12 col-md-12 col-lg-12 text-left course-content">
                @foreach ( $theme->materials as $material )
                  @if ($material->show)
                  <div class="row">
                    <div class="col-height col-xs-12 col-xs-offset-0 col-sm-12 col-sm-offset-0 col-md-1 col-md-offset-1 col-lg-1 col-lg-offset-1">
                      <svg>
                        <image style="height: 20px;" href="{{ $material->icon() }}"/>
                      </svg>
                    </div>
                    <div class="col-xs-12 col-sm-12 col-md-10 col-lg-10">
                      <a target="_blank" href="{{ $material->resource_url }}" class="left">
                        {{ $material->title }}
                      </a>
                    </div>
                  </div>
                  @endif
                @endforeach
              </div>
              @endif
            @endforeach
          </div>
    
    
        </div>
      </section>
    
    
    @endsection
    

    最后,这是我调用所需路线的地方

    @extends('layouts.main')
    
    @section('title')
      My Groups
    @endsection
    
    @php
      $group = $data['group'];
      $units = $data['units'];
    @endphp
    
    @section('content')
    
    <section class="goal-title-section">
      <div class="container white space-at-bottom">
        <div class="row">
          <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 text-center">
            <h1 class="black">{{ $group->name }}</h1>
            <hr class="black">
          </div>
        </div>
        <div class="row">
          <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 text-center">
           <div class="black">
           {!! $group->description !!}
           </div>
           <p class="black">
            This group has the folowing units:
           </p>
          </div>
        </div>
        <section class="units-grid">
    
        @foreach (array_chunk($units, 4) as $unitsRow)
            <div class="row">
                @foreach ($unitsRow as $unit)
                    <div class="col-xs-10 col-xs-offset-2 col-sm-6 col-sm-offset-0 col-md-3 col-md-offset-0 col-lg-3 col-lg-offset-0">
                        <div class="card text-center">
                          <img src="{{ $unit->image }}" alt="Unidad">
                          <h5 class="black">{{ $unit->name }}</h5>
                          <a href="{{ route('user.group.unit.show', ['group'=>$group->id, 'unit'=>$unit->id])}}" class="btn goal-btn goal-btn-blue goal-btn-small">Read more</a>
                        </div>
                    </div>
                @endforeach
            </div>
        @endforeach
    
    </section>
    
      </div>
    </section>
    
    @endsection
    
    1 回复  |  直到 6 年前
        1
  •  6
  •   Mohammed Aktaa    6 年前

    Route::get('/myunits/{group?}/content/{unit?}','Users\GroupContentController@show')->name('user.group.unit.show');