簡體   English   中英

Laravel:缺少必需的參數

[英]Laravel: Missing required parameters

我遇到了missing required parameters的錯誤,它具有以下路由: Route::get('/myunits/{group}/content/{unit}','Users\\GroupContentController@show')->name('user.group.unit.show');

當我們重定向到該路由時,該路由是正確的,但是由於某種原因它失敗了。 但是,當我在GroupContentController@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

您是否可以像這樣進行路由並將dd($ group,$ unit)放入控制器中,以查看發送給它的內容。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM