I need to update records for Guide Steps (Every recipe has steps/instruction and my table was created for storing this data)
I was able to create those records in the db with quite easy syntax:
GuideStep::insert($groupedSteps); // $groupedSteps is an array
But now I need to update those records (and remove unnecessary ones), for this moment I came up with logic that can only update records or create if there is no such records:
foreach ($groupedSteps as $step){
GuideStep::updateOrInsert(
['recipe_id' => $recipeId, 'step_number' => $step['step_number']],
['step_text' => $step['step_text'], 'step_image' => $step['step_image']]
);
}
migration:
Schema::create('guide_steps', function (Blueprint $table) {
$table->id();
$table->foreignId('recipe_id')->constrained()->cascadeOnDelete();
$table->integer('step_number');
$table->text('step_text');
$table->string('step_image')->default('recipes-images/default/default_photo.png');
$table->timestamps();
});
I thought that I’ll be able to use upsert()
but this method requires unique
columns (I don’t have those)
Be grateful for some advices