【问题标题】:Laravel model output customizeLaravel 模型输出自定义
【发布时间】:2017-01-18 20:22:19
【问题描述】:

我的数据库表中有一个名为“name”的列,其中包含这样的 JSON 值:

{"en":"Morocco","ar":"المغرب"}

我想根据当前语言自动访问此值,只需编写:$country->name on my view,谢谢。

【问题讨论】:

    标签: php json laravel laravel-5 laravel-5.3


    【解决方案1】:

    您可以使用accessors 执行此操作。在您的 Country Eloquent 模型中,添加以下内容:

    public function getNameAttribute($value) {
        $name = json_decode($value, true);
        return $name[app()->getLocale()];
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-12
      • 2020-09-24
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      相关资源
      最近更新 更多