pmiranda
pmiranda

Reputation: 8470

User a {id} inside a route on Laravel

I need to put a id number in the middle of an URL to get this:

http://myapp.com/something/29/edit

Now, I have this:

'user_link' => ['here.i.need.to.put.my.route.with.the.number]

My number comes from $event->data->id, is there a way to "concatenate" a number in the middle of a route? Something like

'user_link' => ['something.{id}.edit]
'user_link' => ['something.'.$event->data->id .'.edit] <-- I I do that Laravel shows an error: Route [something.{107}.edit] not defined

The route in question is:

Route::get('something/{id}/edit/', 'MyController@edit')->name('something.edit');

It's possible to put a {id} in the name of the route?

Upvotes: 3

Views: 5841

Answers (2)

Raza Mehdi
Raza Mehdi

Reputation: 941

You shouldn't put an id in a route name. It's bad practice in my opinion. I would suggest that you use route resource.

php artisan make:resource MyController -m Something

This way, you can restful routes:

my/index
my/create
my/store
my/{something}/edit
my/{something}/update
my/{something}/show
my/{something}/destroy

By the above pattern, your edit route will be /my/10/edit, and you data will be automatically fetched.

This way you can use policies to determine that item currently being updated belongs to the logged in user.

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163978

You can use route() helper:

route('something.edit', ['id' => $event->data->id])

Upvotes: 9

Related Questions