You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于语言区域(fr/ar)通过闭包设置关联的title属性

解决方案:根据语言区域动态设置关联选择框的显示字段

你可以通过在relationship方法的第二个参数中传入闭包,动态根据当前语言区域返回对应的字段名,具体实现如下:

Select::make('permis')
    ->relationship(
        'permis',
        fn () => app()->getLocale() === 'ar' ? 'name_ar' : 'name'
    )
    ->preload()
    ->multiple()

补充说明:

  • 闭包中通过app()->getLocale()获取当前应用的语言区域,若需从请求上下文获取语言,也可使用request()->getLocale()
  • 如果需要支持更多语言,可以改用match语法扩展分支:
fn () => match(app()->getLocale()) {
    'ar' => 'name_ar',
    'es' => 'name_es', // 新增西班牙语字段支持
    default => 'name', // 默认 fallback 到法语的name字段
}
  • 请确保你的Permis模型中存在name、name_ar等对应的字段

内容的提问来源于stack exchange,提问作者MENKOUSS Med Abdellahi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 12:14:51