How to pass parameters to the Resource controller directly from the View with Helper url()

Asked

Viewed 1,212 times

2

I have the following route on my Laravel app (v5.5):

Route::resource('tags', 'Painel\TagsController');

In accordance with documentation official, this gives me a route with action Edit, verb GET and URI (/photos/{photo}/Edit). In my View I need to generate a list, example:

<a title="Editar" href="{{ url('/painel/tags/1/edit') }}">
<a title="Editar" href="{{ url('/painel/tags/2/edit') }}">
<a title="Editar" href="{{ url('/painel/tags/3/edit') }}">

Numeric values represent the records returned from the database and printed via loop loop. I can do what I want this way:

<a title="Editar" href="{{ url('/painel/tags/' . $t->id . '/edit') }}">

However, I wanted to see if some Laravel Helper allows you to do this in a more elegant way, something like:

<a title="Editar" href="{{ url('/painel/tags/edit', $t->id) }}">

2 answers

0

There are two other ways you can do this:

Url from a named path (the Resource already does it for us too)

echo route('tags.edit', ['id' => 1]);

// http://example.com/painel/tags/1

Url from the action that controller

action('Painel\TagsController@edit', ['id' => 1]);

// http://example.com/painel/tags/1

With this the Laravel takes care to solve the route for you, in case you are using some Route::group and the like.

0

You can do it using route() in place of url(), but in route() you use the name of the route and not the path... as you used the method Route::resource() these names were given automatically, to see the name of a derminated route you want to use you can use the command php artisan route:list it will return a list with all routes and their information.

In your case as it is a GET for Edit, probably the name will be 'tags.edit' then you can call the route so: route('tags.edit', $t->id)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.