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 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题