davidvera
davidvera

Reputation: 1489

Laravel 5.5 - Passing a variable in the URL with Route::resource?

I try to create a route with Laravel which have a variable in its path. I wrote :

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

In my controller i created an index method :

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'));
}

In my view i have a crud

<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>

My issue is the following is concerning route names :

Route [segments.show] not defined. (View: C:\wamp64\www\network-dev\resources\views\admin\pages\maps\segments\index.blade.php)

While looking to my route list I see this :

The URL is the following : admin/maps/valuechains/{valuechain_id}/segments The route name is : index

App\Http\Controllers\BackOffice\SegmentController@index I should have segments.index instead

Upvotes: 0

Views: 829

Answers (1)

davidvera
davidvera

Reputation: 1489

I just had to remove a "/" replacing 'segments/' by 'segments' in my route:

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

The correct writting was :

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

Upvotes: 0

Related Questions