【问题标题】:The scoping nested resources feature of the Laravel Nested Resources will not checking the child model is belongs to the parent model or notLaravel 嵌套资源的作用域嵌套资源功能不会检查子模型是否属于父模型
【发布时间】:2021-09-23 08:34:22
【问题描述】:

我正在开发具有nested resources URL 的 Laravel 8 应用程序。

根据我的路由定义,一个用户可以有多个位置,每个位置可以有多个内容。

Route::resource('locations', LocationController::class);
Route::resource('locations.contents', ContentController::class);

所以当我打开网址时

/locations/3/contents/1

它永远不会检查 id 为 1 的内容是否属于其父级(即 id 为 3 的位置)。

我期待的是,它应该由 Laravel 框架返回 404 页面,因为内容模型(id 1)实际上属于位置模型(id 2)

总之

/locations/1/contents/1 - 应该可以工作

/locations/3/contents/1 - 不应该工作。

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: laravel scope nested-routes


    【解决方案1】:

    通过指定路由的范围选项,它开始工作。

    我必须设置范围,即使它应该由框架自动设置。

    Laravel documentation提及。

    路线应该是

    Route::resource('locations', LocationController::class);
    Route::resource('locations.contents', ContentController::class)->scoped([
        'locations' => 'location:id',
        'contents' => 'content:id',
    ]);
    

    【讨论】:

      猜你喜欢
      • 2016-11-27
      • 2016-12-30
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多