douzuqin3467 2017-11-05 08:47
浏览 51
已采纳

使用仅ID的两个变量输出路径url

If possible..how would this be done? Laravel 5.5

route('section.category.subcategory',$subcategory->id) 

must output (routes/web.php has the get:: set as this too)

/section/{parent_slug}/{subcategory_slug}

I could easily do

route('section.category.subcategory',[
                                     'subcategory_slug' => $subcategory->slug, 
                                     'parent_slug'=>$parent->slug
                                     ]
);

but I'm trying to avoid having to declare those things all the time.

I thought getRouteKeyName in model would be first place to go to, but it binds to only one variable as far as I could find.

RouteHandler isn't the place to do anything either because it reads the url, not outputs it right?

I'm assuming in some file that I don't know about I will have to set this sort of logic.

if(requested_route is section.category.subcategory)){
   // get the parent_id of ID provided,
   // get parent's slug
   // build the url.
}

Even better, I think I could do a left join when pulling the list of subcategories, so I have $subcategory->parent_slug instead of going for $parent->slug. This way

route('section.category.subcategory',[$subcategory]) 

has all the variables it needs to work with.

  • 写回答

1条回答 默认 最新

  • dqssst0144 2017-11-05 10:47
    关注

    I think for this helper would be a good choice so you could create url like this:

    route('section.category.subcategory',build_category_parameters($subcategory)) 
    

    and you can create function like this:

    function build_category_parameters($subcategory)
    {
       // here any logic that is needed
    
       // here return array something like this
       return [ 'subcategory_slug' => $subcategory->slug, 
                'parent_slug'=> $subcategory->parent->slug
              ];
    }
    

    If you don't have helper file already you can create one using this way Laravel - require php script in a service provider

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测