F
Filament10mo ago
Wirkhof

How to turn off casting during seeding?

I do find the $casts causing me troubles during development and constant reseeding . I have this:
protected $casts = [
'brand' => 'array'
];
protected $casts = [
'brand' => 'array'
];
but it causes issues when I import stuff from my csv file and the quotes and [ are messed up. If I comment it off:
protected $casts = [
// 'brand' => 'array'
];
protected $casts = [
// 'brand' => 'array'
];
. it will be imported from the csv as it should. But then I have to manually uncomment the casting in the model so the tables and forms reflect and read the values. Otherwise, for example, the columns will be empty or the form select field will look like nothing is selected. So, because of this procedure I sometimes I forget to do it in the correct order and the csv file is big so it is getting frustrating. Is there a way I could use a seeding specific $casting and a normal usage casting somehow left alone - without the need to comment before importing and uncomment after importing? For example can I overwrite the model's casting setting in the seeder file?
Solution:
via eleoquent i guess so try something like this:
$brand = new Brand();
$brand->setRawAttributes(["your"=>"attributes"])
$brand->save();
$brand = new Brand();
$brand->setRawAttributes(["your"=>"attributes"])
$brand->save();
...
Jump to solution
4 Replies
Forever
Forever10mo ago
How Do you Seed ?
Wirkhof
WirkhofOP10mo ago
$this->call(... and seeder classes here)
Solution
Forever
Forever10mo ago
via eleoquent i guess so try something like this:
$brand = new Brand();
$brand->setRawAttributes(["your"=>"attributes"])
$brand->save();
$brand = new Brand();
$brand->setRawAttributes(["your"=>"attributes"])
$brand->save();
Wirkhof
WirkhofOP10mo ago
Ok, interesting. I am using Car::create(... and then array of things I want to store) so I will try setRawAttributes and report back great! Seems to be working nice. Thank you
Want results from more Discord servers?
Add your server