I have used the following insert array to insert multiple entries in Laravel Eloquent ORM
$i = 0;
foreach($exerciseValArray as $kkey=>$exerciseValue){
if($exerciseValue['exercise'] =='' || $exerciseValue['section_type'] == ''){
return Response::JSON(array('errors'=>array('Error in workout exercise section')));
}
$exerciseData = explode(',',$exerciseValue['exercise']);
foreach($exerciseData as $exerciseid){
$insertArray[$i]['fk_workouts_id'] = $id;
$insertArray[$i]['fk_users_id'] = $userId;
$insertArray[$i]['fk_exercises_id']= $exerciseid;
$insertArray[$i]['section'] = $exerciseValue['section_type'];
$insertArray[$i]['status'] = 1;
$insertArray[$i]['item_index'] = $index+$kkey+1;
$insertArray[$i]['updated_at'] = $workoutDetails['updated_at'];
$i++;
}
}
WorkoutExercise::insert($insertArray);
The above code works fine and inserts the data array in to the database.
I have learnt the usage of Model::updateOrCreate and used it in some of my modules successfully (i.e)
Model::updateOrCreate($attributes = array('key' => 'value'), $values = array('key' => 'value'));
My question is how to edit the above insert snippet so that I could use Model::updateOrCreate in it. My $attributes fields are 'fk_workouts_id','fk_users_id','fk_exercises_id' and the rest in the $values to be changed.
I have no idea how to implement this. Pls help...
insertmethod is in factQuery\Builders and it doesn't use Eloquent features at all (eg. timestamps and so on).EloquentorQuery\Builderthat I could create entry or update if data presence @JarekTkaczyk . Because I need to updateOrCreate nearly 20-50 data is it a good practice to do it one by one??firstOrNewmethod, which is the way to go.