Eloquent 将属性转换为集合的意外行为

Eloquent casting attributes to Collections unexpected behaviour

Q1. 我有一个 Eloquent 模型,可以将属性转换为集合。 在此属性上调用 Collection 的方法不会影响模型值。例如:put()

使用 Collections 时,我可以做到这一点:

$var = collect();
$var->put('ip', '127.0.0.1');
var_dump($var);

按预期输出:

object(Illuminate\Support\Collection)[191] protected 'items' => array (size=1) 'ip' => string '127.0.0.1' (length=4)

但是当我在 Eloquent 模型上使用铸造属性时,这并没有像预期的那样工作

$user = App\User::create(['email'=>'Name', 'email'=>'mail@example.com', 'password'=>bcrypt('1234')]);
$user->properties = collect();
$user->properties->put('ip', '127.0.0.1');
var_dump($user->properties);

object(Illuminate\Support\Collection)[201] protected 'items' => array (size=0) empty

这不会填充该字段。 我认为已创建另一个集合,因此要按预期工作,我必须将这个新集合分配给我的字段。

像这样: $user->properties = $user->properties->put('ip', '127.0.0.1');

Q2.有没有合适的方式来默认初始化字段的集合(如果字段为null则创建一个空集合),而不必调用$user->properties = collect(); "manually"每次?


User.php

class User extends Authenticatable
{
    protected $casts = [
        'properties' => 'collection',
    ];
    ...
}

迁移文件

Schema::table('users', function($table) {
    $table->text('properties')->nullable();
});

Q1:转换为集合的属性具有 getter returns、每次,一个new BaseCollection是根据属性的值构造的。

如前所述,getter returns 另一个 集合实例及其上的每个直接更改不会更改 属性 的值,而是新创建的集合对象。

正如您所指出的,设置集合转换属性的唯一方法是为其分配自己的原始值并与新值合并。

因此,您必须使用:

而不是 put()
$user->properties = $user->properties->put('ip', '127.0.0.1');
// or
$user->properties = $user->properties ->merge(['ip'=>'127.0.0.1'])

Q2:我们不得不认为数据库表示是一个text;所以恕我直言,在 migration 中初始化 Model 的正确方法是给它一个默认的 empty json,即:

$table->text('properties')->default('{}');

但这适用于在未设置属性字段的情况下创建的模型和之后检索的模型。

对于新创建的 模型,我的建议是传递默认值void array,即:

 App\User::create([
     'name'=>'Name', 
     'email'=>'mail@example.com', 
     'password'=>bcrypt('1234'),
     'properties' => []
 ]);

除了dparoli的出色回答外,还可以通过Laravel的启动方法添加默认值,每个型号都可用。

类似下面的示例代码

   protected static function boot()
   {
      parent::boot(); //because we want the parent boot to be run as well
      static::creating(function($model){
         $model->propertyName = 'propertyValue';
      });
    }

如果您愿意,也可以尝试这种方法。