TC
Twill CMS•3y ago
daniel

Issue with locales and creating model records from a custom page

I've built a CSV Importer using a custom page, and have gotten to the point where I can successfully upload the file and start creating new records with my model/repository. But I'm running into an issue when it's trying to create the slug and translation data. Specifically, the active column is not being set, and the `HasSlug::getSlugParams() method doesn't know what to do with that since it's detecting that I should have locale info. I can't figure out how to send along locale information from my custom page, or if there's another way to send or bypass this from my code to create the new record? I hope that makes sense. I'm on Twill 2.11.0
11 Replies
kallefrombosnia
kallefrombosnia•3y ago
Hi, this package is very useful for exporting data, but I found that it can be complicated to work. But its not immposible. That package offers https://docs.laravel-excel.com/3.1/imports/collection.html which gets you an collection of records which you want to import. After that you can use your Dealer Repository to create a new record.
app(DealerRepository::class)->create([
'title' => $titles,
'description' => $descriptions,
'languages' => $cms,
'published' => false,
]);
app(DealerRepository::class)->create([
'title' => $titles,
'description' => $descriptions,
'languages' => $cms,
'published' => false,
]);
This snippet is from my own code. Best way to replicate this to override in your repo method prepareFieldsBeforeSave and dump or dd your incoming fields from frontend form. I explained that here https://discord.com/channels/811936425858695198/1040634143005999126
Importing to collections | Laravel Excel
Supercharged Excel exports and imports in Laravel
kallefrombosnia
kallefrombosnia•3y ago
languages and published are default fields. Languages contains array of locales which have 1 or 0 and that is used for the active field.
public function collection(Collection $rows)
{
foreach ($rows as $row)
{
app(DealerRepository::class)->create([
'title' => ['en' => 'Title EN', 'fr' => 'Title FR'],
'description' => ['en' => '', 'fr' => 'Baguette'], // Need to append locales even if they are empty ''
'languages' => ['en' => 1, 'fr' => 0], // its only active for en locale
'published' => false, // depends on your use case
]);
}
}
public function collection(Collection $rows)
{
foreach ($rows as $row)
{
app(DealerRepository::class)->create([
'title' => ['en' => 'Title EN', 'fr' => 'Title FR'],
'description' => ['en' => '', 'fr' => 'Baguette'], // Need to append locales even if they are empty ''
'languages' => ['en' => 1, 'fr' => 0], // its only active for en locale
'published' => false, // depends on your use case
]);
}
}
daniel
danielOP•3y ago
ahhh ok
kallefrombosnia
kallefrombosnia•3y ago
What Im not familiar in this case are slugs, you will need to check that on your own. translates and all other fields will be resolved like this
daniel
danielOP•3y ago
i think if i pass the language information as you've shown there that should take care of the slugs but i'll try this out and see
kallefrombosnia
kallefrombosnia•3y ago
Only some additional relathionships like media, files, tags needs to be added via relation to this, but since you dont need that, Ill will not write about that.
daniel
danielOP•3y ago
I'm getting this error when attempting to import now, something to do with the languages being published? i tried to pass values for published with a language array but that didn't work
Hro
Hro•3y ago
Just a tip in general, you can use the browser inspector to see in what form the data is being sent. From there you can deduct what structure you need to pass to the create method.
daniel
danielOP•3y ago
thanks @Harings Rob and @kalle, my import is working now!
kallefrombosnia
kallefrombosnia•3y ago
If you dont use description, you can remove it. That was just for an example.
daniel
danielOP•3y ago
ha good catch, i should be using it 🙂 thanks again!
Want results from more Discord servers?
Add your server