如何在Livewire中传递属性?

时间:2020-09-29 19:35:38

标签: php laravel laravel-livewire

我的应用程序是多种语言,并且在Livewire组件中进行了如下验证:

$this->validate([
    'name' => 'required|string|max:200',
    'desc' => 'required|string|max:2000',
],[],[
    'name' => trans('site.name'),
    'desc' => trans('site.description'),
]);

我需要通过trans函数来翻译输入的属性

使用上面的代码,我收到此错误:

ErrorException
mb_strtoupper() expects parameter 1 to be string, array given 

当我删除trans时,验证就可以了

如何在livewire中翻译attributes

更新

站点文件:

en / site.php

return [
  'name'=> 'name',
  'description'=> 'Description'
];

ar / site.php

return [
  'name'=> 'الاسم',
  'description'=> 'الوصف'
];

1 个答案:

答案 0 :(得分:0)

我尝试了问题中提供的代码。导致mb_strtoupper() expects parameter 1 to be string, array given 异常的原因是trans('site.name')trans('site.descrption')返回一个数组。代码没有错。这是因为site.php文件中的键之一包含一个数组。我可以使用以下代码重现相同的错误(我有目的地返回了一个数组)。因此,请检查site.php文件中的键。只需将其替换为字符串即可解决此错误。

  $this->validate([
            'name' => 'required|string|max:200',
            'desc' => 'required|string|max:2000',
        ], [], [
            'name' => [],
            'desc' => trans('site.descrption'),
        ]);

enter image description here