laravel 5 route passing two parameters

2019-09-07 01:41发布

问题:

i have this route

Route::get('/artist/{id}/{name}', 'HomeController@artist')->where(['id' => '[0-9]+', 'name' => '[a-z]+'])->name('artist');

and this is my link

<a href="{{route('artist',$artist->id,$artist->name)}}">{{$artist->name}}</a>

and this is the artist method on HomeController

public function artist($id, $name){ $artist = Artist::where('id', $id)->where('name', $name)->first();

    return view('front.artist', compact('artist'));
}

i donot know this display error. this is the error. so please any one help me with this. iam in the middle of learning laravel.

ErrorException in UrlGenerationException.php line 17:
Missing required parameters for [Route: artist] [URI: artist/{id}/{name}]. (View: C:\xampp\htdocs\laravel\resources\views\front\home.blade.php)

回答1:

Yo must pass the parameters as array, see https://laravel.com/docs/5.4/helpers#method-route

route('artist',['id' => $artist->id, 'name' => $artist->name])

or you can use

{!! link_to_route('artist', $artist->name, ['id' => $artist->id, 'name' => $artist->name]) !!}