我想从表单中选择和存储多个数据--输入名为"property_type",但我得到了‘从数组到字符串转换’的错误:这是我的迁移:
public function up() {
Schema::create('projects', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('ref')->nullable();
$table->string('name');
$table->string('community');
$table->text('property_type')->nullable();
$table->integer('floor_number')->nullable();
});
}我的模特:
class Project extends Model implements HasMedia {
protected $fillable = [
'ref',
'name',
'community',
'property_type',
'floor_number',
];
public function setPtypeAttribute($value) {
$this->attributes['property_type'] = json_encode($value);
}
/**
* Get the categories
*
*/
public function getPtypeAttribute($value) {
return $this->attributes['property_type'] = json_decode($value);
}意见:
<form enctype="multipart/form-data" method="POST" novalidate action="{{ route("admin.projects.store") }}" >
@csrf
<div class="form-group">
<label class="required">property_type</label>
<select class="form-control select2 name="property_type[]" id="property_type" required multiple="">
<option value="php">PHP</option>
<option value="react">React</option>
<option value="jquery">JQuery</option>
<option value="javascript">Javascript</option>
<option value="angular">Angular</option>
<option value="vue">Vue</option>
</select>
</div>我的店面要求:
'property_type' => [
'array',
'nullable',
],我需要帮助!
发布于 2021-12-25 13:06:12
在您的模型中修复像这样的访问器和变异器。这些函数区分大小写。
public function getPropertyTypeAttribute($value)
{
return json_decode($value);
}
public function setPropertyTypeAttribute($value)
{
$this->attributes['property_type'] = json_encode($value);
}如果您使用的是Laravel8.77或更高版本,您可以像下面这样使用它们。
use Illuminate\Database\Eloquent\Casts\Attribute;
protected function propertyType(): Attribute
{
return new Attribute(
fn($value) => json_decode($value),
fn($value) => json_encode($value)
);
}第一个参数是getter,第二个参数是setter。
发布于 2021-12-25 08:43:32
https://stackoverflow.com/questions/70478730
复制相似问题