Aleš Chromec
Aleš Chromec

Reputation: 255

Laravel route with id and name

I generate URL like this:

URL::action('FieldsController@show',['id' => $field->id, 'head' => cleanUrl($field->head)])

In my routes I have:

Route::get('/field/{head}-{id}', 'FieldsController@show');

And it dont work, only when I put ID first and HEAD second like this:

Route::get('/field/{id}-{head}', 'FieldsController@show');

Anyone have ideas? I need to have ID after HEAD in URL

Upvotes: 3

Views: 20161

Answers (2)

Alfred
Alfred

Reputation: 1

I am using it like this and it works just fine:

Route::get('/blog/{slug}-{article}', [App\Http\Controllers\BlogController::class, 'article'])->name('blog.article')->where(['slug' => '[\w]+[^\d]+', 'article' => '[\d]+$']);

Upvotes: 0

Laurence
Laurence

Reputation: 60038

You cant do routing like

{head}-{id}

You need to do this:

Route::get('/field/{head}/{id}', 'FieldsController@show');

Then in your show() function you can combine them yourself:

function show($head, $id)
{
     $var = $head.'-'.$id;
     // do whatever you want with $var here
}

Upvotes: 7

Related Questions