database - On existing record skip otherwise insert data Laravel Eloquent -
i have following table structure seed dynamically api
public function up() { schema::create('reports', function(blueprint $table) { $table->engine = 'innodb'; $table->increments('id'); $table->integer('program_id')->unique(); $table->date('date'); . . . $table->timestamps(); });
on each api request array of data , try save theme in table above. if program_id exist ignore records , save new one. i've been looking around not find best way validate specific value in retrieved array
sqlstate[23000]: integrity constraint violation: 1062 duplicate entry '528027827' key 'reports_program_id_unique
the way save records
// $datas field datas api foreach ($datas $data) { $report = \td\reports\reports\reports::firstorcreate($data); }
how skip insert on existing program_id , insert new ones?
you can use firstorcreate()
:
$data = ['program_id' => 528027827, ...]; $report = report::firstorcreate($data);
Comments
Post a Comment