【问题标题】:Laravel error : Array to string conversionLaravel 错误:数组到字符串的转换
【发布时间】:2022-01-25 10:18:51
【问题描述】:

我想从我的表单中选择并存储多个数据,输入名称是“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',
        ],

我需要帮助!

【问题讨论】:

  • 访问者真的叫getPtypeAttribute吗?不应该叫getPropertyTypeAttribute吗? (大小写也很重要)
  • 我改变了它,现在它存储在数据库中但是,在索引中我得到 json_decode() 期望参数 1 是字符串,给定数组(查看:C:\xampp\htdocs\OPEN- CRM-1\resources\views\admin\projects\index.blade.php)
  • 如果您的 getter 和 mutator 正常工作,那么您只需要在其中使用 json_encodejson_decode 而在代码中的其他地方不需要它们
  • 是的,我没有在任何地方使用过,我只是在 getter 和 setter 中使用过!

标签: php mysql laravel


【解决方案1】:

在你的模型中像这样修复你的访问器和修改器。这些函数区分大小写

public function getPropertyTypeAttribute($value)
{
    return json_decode($value);
}

public function setPropertyTypeAttribute($value)
{
    $this->attributes['property_type'] = json_encode($value);
}

如果你使用的是 Laravel 8.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。

【讨论】:

    【解决方案2】:

    在 laravel 中使用 castscasts in laravel

    将此添加到Project 模型:

    protected $casts = [
            'property_type' => 'array',
        ];
    

    【讨论】:

      猜你喜欢
      • 2019-05-01
      • 2016-09-01
      • 1970-01-01
      • 2014-12-14
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      相关资源
      最近更新 更多