Laravel v:5.7
PHP v:7.2.10
路由路径为:admin/apartments/{apartment}/associations/{association}/association-users/{association_user}
获取网址:http://127.0.0.1:8000/admin/apartments/1/associations/1/association-users
枢轴模型:AssociationUser
在App\Providers\RouteServiceProvider
中,我已添加
public function boot()
{
parent::boot();
Route::bind('association-user', function ($value) {
return App\pivotes\AssociationUser::where('association_id', request()->route()->parameter('association')->id)->where('user_id', auth()->id())->first() ?? abort(404);
});
}
路线创建
route('apartments.associations.association-users.show', ['apartment' => $associationUser->association->apartment, 'association' => $associationUser->association, 'association_user' => $associationUser ])
答案 0 :(得分:0)
如果我没有记错的话,association_user数据透视表应该具有association_id和user_id,并且两者的组合将是唯一的,因此在您的路由中已经有{association}
模型,因此我相信您可以使用
public function getRouteKeyName()
{
return 'user_id';
}
在透视模型类中,以便user_id出现在您的URL中,并且您将结合使用关联和用户模型。
您不需要
Route::bind('association-user', function ($value) {
return App\pivotes\AssociationUser::where('association_id', request()->route()->parameter('association')->id)->where('user_id', auth()->id())->first() ?? abort(404);
});
在您的App\Providers\RouteServiceProvider