dthp96899 2018-08-07 15:37
浏览 18

如何在Laravel的路线中建立雄辩的关系?

I am creating an application in Laravel which has 3 models with relationships to each other.

I have got a problem in routing where I show particular category according to the id. I do like this:

Route::apiResource('/categories', 'Core\CategoryController');

Route::prefix('categories')->group(function() {
    Route::apiResource('/{category}/sub_categories', 'Core\SubCategoryController');

    Route::prefix('sub_categories')->group(function() {
        Route::apiResource('/{sub_category}/sub_sub_categories', 'Core\SubSubCategoryController');
    });
});

I am able to do http://localhost:8080/api/categories/3/sub_categories. However, I am not able to do http://localhost:8080/api/categories/3/sub_categories/1/sub_sub_categories. I am not able to get the id of category and display it in url. What I am able to do is this -> http://localhost:8080/api/categories/sub_categories/1/sub_sub_categories

So, how can I display the id of category in that URL?

I tried:

Route::prefix('/{category}/sub_categories')->group(function() {
    Route::apiResource('/{sub_category}/sub_sub_categories', 'Core\SubSubCategoryController');
});

However, that didn't work as expected.

  • 写回答

1条回答 默认 最新

  • dongzheng3113 2018-08-07 16:04
    关注

    You can do it like this

    Route::prefix('categories')->group(function() {
        Route::apiResource('/{category}/sub_categories/{sub_category}/sub_sub_categories', 'Core\SubSubCategoryController');
        Route::apiResource('/{category}/sub_categories', 'Core\SubCategoryController');
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么