Laravel 5.5 - 使用Route :: resource在URL中传递变量?

时间:2018-05-27 12:21:59

标签: routes laravel-5.5 laravel-blade

我尝试用Laravel创建一条路径,路径中有一个变量。 我写道:

Route::resource('/maps/valuechains/{valuechain_id}/segments/', 'BackOffice\SegmentController');

在我的控制器中,我创建了一个索引方法:

public function index(EntityRepository $vcs, $valuechain_id)
{
    $entitiesLists = $vcs->getEntities();

    $segments = Segment::select()
        ->join('valuechains', 'segments.valuechain_id', 'valuechains.id')
        ->join('lang_segment', 'segments.id', 'lang_segment.segment_id')
        ->join('langs', 'langs.id', 'lang_segment.lang_id')
        ->join('admins', 'segments.admin_id', 'admins.id')
        ->where([
            ['langs.isMainlanguage', '=', 1],
            ['valuechains.id', '=', $valuechain_id]
        ])
        ->get();

    $segmentCount = Segment::count();
    return view('admin.pages.maps.segments.index', compact('segments', 'segmentCount', 'entitiesLists'));
}

在我看来,我有一个小问题

<span style="overflow: visible; width: 110px;">
    <span>
        <a href="{{ route('segments.show', $segment->id) }}"
           class="m-portlet__nav-link btn m-btn m-btn--hover-accent m-btn--icon m-btn--icon-only m-btn--pill"
           title="View details">
             <i class="la la-eye"></i>
         </a>
       <a href="{{ route('segments.edit', $segment->id) }}"
           class="m-portlet__nav-link btn m-btn m-btn--hover-accent m-btn--icon m-btn--icon-only m-btn--pill"
           title="Edit details">
           <i class="la la-edit"></i>
        </a>
        {!!
           Form::open([
              'method'    => 'DELETE',
              'route'     => ['segments.destroy', $segment->id]
           ])
        !!}
        {!!
           Form::submit(
              ' ',
             [
              'class' => 'la la-trash m-portlet__nav-link btn m-btn m-btn--hover-danger m-btn--icon m-btn--icon-only m-btn--pill',
              'title' => 'Delete'
             ]
           )
        !!}
        {!! form::close() !!}
    </span>
</span>

我的问题是关于路线名称:

路线[segmentss.show]未定义。 (查看:C:\ wamp64 \ www \ network-dev \ resources \ views \ admin \ pages \ maps \ segments \ index.blade.php)

在查看我的路线列表时,我看到了:

网址如下: 管理/映射/ valuechains / {valuechain_id} /区段 路线名称为:index

应用\ HTTP \控制器\的BackOffice \ SegmentController @索引 我应该有segments.index而不是

1 个答案:

答案 0 :(得分:0)

我只需删除&#34; /&#34;替换&#39;段/&#39;通过&#39;细分&#39;在我的路线:

Route::resource('/maps/valuechains/{valuechain_id}/segments/', 'BackOffice\SegmentController');

正确的写作是:

Route::resource('/maps/valuechains/{valuechain_id}/segments', 'BackOffice\SegmentController');