Laravel 5 Laravel雄辩DB播种机和阵列铸造

Laravel 5 Laravel雄辩DB播种机和阵列铸造,laravel-5,eloquent,Laravel 5,Eloquent,如果我使用DB seeder插入数据(并检索数据),有人能帮助我理解如何使用属性转换吗 这是播种机的一部分 DB::table('events')->insert([ [ 'title' => 'prizes', 'description' => serialize([ "text" => "General description..."

如果我使用DB seeder插入数据(并检索数据),有人能帮助我理解如何使用属性转换吗

这是播种机的一部分

    DB::table('events')->insert([
    [
            'title' => 'prizes',
            'description' => serialize([
                    "text" => "General description..."
                    ]),
            'language' => 'en',
            'time_start' => null,
            'time_end' => null,
            'lat' => null,
            'lng' => null,
    ]
]);
通过这种方式,我的数据库中确实有序列化数据,但当我这样做时

$events = Event::with('user')->where('child','=',null)->get();
我得到的描述是空的

(不必担心用户默认为空)

这是我的事件模型

class Event extends Model
{
    protected $table = 'events';
    //
    public function user()
    {
        return $this->hasOne('App\User','id');
    }
    protected $casts = [
        'description' => 'array',
    ];
}
我在这里遗漏了什么?

根据这篇文章,雄辩的数组转换将数组序列化为JSON:

当处理存储为序列化JSON的列时,数组转换类型特别有用

所以你应该像这样插入它:

DB::table('events')->insert([[
    'title' => 'prizes',
    'description' => json_encode(["text" => "General description..."]),
    'language' => 'en',
    'time_start' => null,
    'time_end' => null,
    'lat' => null,
    'lng' => null,
]]);
作为旁注,它属于PHP核心,不生成JSON。

根据以下内容,雄辩的数组转换将数组序列化为JSON:

当处理存储为序列化JSON的列时,数组转换类型特别有用

所以你应该像这样插入它:

DB::table('events')->insert([[
    'title' => 'prizes',
    'description' => json_encode(["text" => "General description..."]),
    'language' => 'en',
    'time_start' => null,
    'time_end' => null,
    'lat' => null,
    'lng' => null,
]]);

作为旁注,它属于PHP核心,不产生JSON。

试试
JSON\u encode
。@thefallen是这样工作的,thanksTry
JSON\u encode
。@thefallen是这样工作的,谢谢