Seeding Data - Check exists, if NO insert, if YES grab ID, plus run on multiple inserts
Created 6 years ago by garethshaw

Hi All

So other than:

    if (!$logo = $this->folders->findBySlug('logos')) {
        $this->folders->create(...

Is there a single command I can run that is more efficient that a select then insert? Especially when I'm running this over 300 records. See my other question about multiple inserts. Can this be done with multi-inserts and catch errors?

So for example I have 2 modules called Cameras and Lenses seeding Makes on install: Cameras has Canon, Nikon and Hassleblad Models has Canon, Nikon, Hassleblad and Sigma

Camera module is installed first and adds 3 records to the empty Makes table, grabbing each ID. Next Models installs, ignores the first 3 grabbing their IDs but inserts Sigma.

Thanks Gareth

piterden  —  6 years ago

vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php#L341

findOrNew()

ryanthompson  —  6 years ago

300 inserts really isn't that big of a deal. Specially when you're seeding. I've written importers I ran / reran to sync that were basically seeders that worked with 300,000 records. It wasn't a blink of the eye but at some point it is what it is. I would be more concerned if it would be part of the business logic of the project. Then you would need to look at queueing and what not but for seeding / importing / etc.. 300 isn't an issue.

I also like cycling over 1 at a time cause in your seeder you can probably export logs easier. And if you use a console command for say an importer - you can export debugger lines / progress bar in the CLI terminal too.