Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有相同端点但具有多个模型的Laravel模型绑定_Laravel_Model_Model Binding - Fatal编程技术网

具有相同端点但具有多个模型的Laravel模型绑定

具有相同端点但具有多个模型的Laravel模型绑定,laravel,model,model-binding,Laravel,Model,Model Binding,我面临的挑战是如何解决与两种不同模型的模型绑定: Route::get('/{community:slug_url}/events', 'Community\CommunityController@events')->name('communityEvents'); Route::group(['prefix' => '{user:username}'], function ($username) { Route::get('events', 'Events\EventCo

我面临的挑战是如何解决与两种不同模型的模型绑定:

Route::get('/{community:slug_url}/events', 'Community\CommunityController@events')->name('communityEvents');

Route::group(['prefix' => '{user:username}'], function ($username) {
    Route::get('events', 'Events\EventController@listEvents')->name('events');
});
它对第一条路线有效,但对第二条路线无效,因为找不到404

我在这里读到了关于显式模型绑定的代码,但不确定我还缺少什么

Route::bind('user', function ($value) {
    return User::where('username', $value)->firstOrFail();
});

Route::bind('community', function ($value) {
    return Community::where('slug_url', $value)->firstOrFail();
});
此外,在各个型号中设置以下各项:

Model **User**
public function getRouteKeyName()
{
    return 'username';
}

Model **Community**
public function getRouteKeyName()
{
    return 'slug_url';
}
谢谢你的帮助。 谢谢