语法错误,意外的“,”,期待“]”
syntax error, unexpected ',', expecting ']'
我正在尝试将数据添加到数据库中,所有第一个单词都是大写字母。但它显示错误语法错误。
unexpected ',', expecting ']',,,,,,,,,,,,,,,,,,,
型号
protected $fillable = [
'category_name',
'category_description',
'category_slug',
'category_image'
];
public function setFirstNameAttribute($value)
{
$this->attributes[
'category_name',
'category_description',
'category_slug',
'category_image'
] = strtoupper($value);
}
试试吧。
public function setCategoryNameAttribute($value){
$this->attributes['category_name'] = strtoupper($value);
}
或
public function setCategoryNameAttribute($value)
{
return strtoupper($value);
}
如果您想对所有字段进行处理,那么您可能已经为所有字段定义了函数。
$this->attributes[
'category_name',
'category_description',
'category_slug',
'category_image'
] = strtoupper($value);
您不能像这样一次设置多个数组值。您必须单独或循环设置它们:
public function setFirstNameAttribute($value)
{
$attribute_names = [
'category_name',
'category_description',
'category_slug',
'category_image',
];
foreach ($attribute_names as $attribute_name) {
$this->attributes[$attribute_name] = strtoupper($value);
}
}
您对 setFirstNameAttribute($value)
增变函数的使用是错误的。 Mustator 仅适用于一个属性。所以你必须为每个属性创建多个修改器。
可能的答案。如果你想改变 category_name
.
public function setCategoryNameAttribute($value)
{
$this->category_name = strtoupper($value);
}
如果你想变异 category_description
public function setCategoryDescriptionAttribute($value)
{
$this->category_description = strtoupper($value);
}
同样,您需要为所有其他属性定义多个修改器。
编辑
如果您只想将单词的第一个字母大写,请使用 title_case()
而不是 strtoupper()
public function setCategoryDescriptionAttribute($value)
{
$this->category_description = title_case($value);
}
我正在尝试将数据添加到数据库中,所有第一个单词都是大写字母。但它显示错误语法错误。
unexpected ',', expecting ']',,,,,,,,,,,,,,,,,,,
型号
protected $fillable = [
'category_name',
'category_description',
'category_slug',
'category_image'
];
public function setFirstNameAttribute($value)
{
$this->attributes[
'category_name',
'category_description',
'category_slug',
'category_image'
] = strtoupper($value);
}
试试吧。
public function setCategoryNameAttribute($value){
$this->attributes['category_name'] = strtoupper($value);
}
或
public function setCategoryNameAttribute($value)
{
return strtoupper($value);
}
如果您想对所有字段进行处理,那么您可能已经为所有字段定义了函数。
$this->attributes[
'category_name',
'category_description',
'category_slug',
'category_image'
] = strtoupper($value);
您不能像这样一次设置多个数组值。您必须单独或循环设置它们:
public function setFirstNameAttribute($value)
{
$attribute_names = [
'category_name',
'category_description',
'category_slug',
'category_image',
];
foreach ($attribute_names as $attribute_name) {
$this->attributes[$attribute_name] = strtoupper($value);
}
}
您对 setFirstNameAttribute($value)
增变函数的使用是错误的。 Mustator 仅适用于一个属性。所以你必须为每个属性创建多个修改器。
可能的答案。如果你想改变 category_name
.
public function setCategoryNameAttribute($value)
{
$this->category_name = strtoupper($value);
}
如果你想变异 category_description
public function setCategoryDescriptionAttribute($value)
{
$this->category_description = strtoupper($value);
}
同样,您需要为所有其他属性定义多个修改器。
编辑
如果您只想将单词的第一个字母大写,请使用 title_case()
而不是 strtoupper()
public function setCategoryDescriptionAttribute($value)
{
$this->category_description = title_case($value);
}