mercredi 19 juin 2019

Laravel Insert Seed if data doesn't already exist

Is there any way to run a laravel seed to only insert the records if they do not exist already?

My current laravel seeder looks like this

DB::table('users')->insert([
                //Global Admin
                [
                    'member_id' => 11111111,
                    'firstname' => 'Joe',
                    'lastname' => 'Bloggs'
                ], 
                [
                    'member_id' => 22222222,
                    'firstname' => 'Jim',
                    'lastname' => 'Bloggs'
                ], 

            ]);

Pretty standard!

would I have to wrap each and every insert in a try catch? like this

try {
   DB::table('users')->insert(['member_id' => 11111111, 'firstname' => 'Joe', 'lastname' => 'Bloggs']);
} catch(Exception $e){
  //Die silently
}
try {
   DB::table('users')->insert(['member_id' => 22222222, 'firstname' => 'Jim', 'lastname' => 'Bloggs']);
} catch(Exception $e){
  //Die silently
}



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2x1OL0Z
via IFTTT

Aucun commentaire:

Enregistrer un commentaire