jeudi 14 novembre 2019

How to seed json field on laravel 5.8 and postgresql

I am trying to seed table with json fields. I use :

$table->json('myfield1');
$table->json('myfield2');
$table->json('myfield3');

I added $casts attributes to model like this

protected $casts = [
    'myfield1' => 'array',
    'myfield2' => 'array',
    'myfield3' => 'array'
];

Then in my seeder I try:

$data = [ 'myfield1' => ["toto","tata"], 'myfield2' => ["cat","dog"], 'myfield3' => ["toyota","renauld"] ]
$preferences = Preference::firstOrCreate($data); //not working and I am getting **Invalid parameter number: parameter was not defined** 

Try this too:

$data = [ 'myfield1' => json_encode(["toto","tata"]), 'myfield2' => json_encode(["cat","dog"]), 'myfield3' => json_encode(["toyota","renauld"]) ]
$preferences = Preference::firstOrCreate($data); //not working and getting **operator does not exist: json = unknown** followed by **No operator matches the given name and argument types. You might need to add explicit type casts.**

Spending many hours without solution or tricks, any help will be welcome, thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire