Using updateOrCreate to enter multiple data in Laravel

I used the following insert array to insert multiple records in a 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 an array of data into the database.

I found out about using Model::updateOrCreate and successfully used it in some of my modules (e.g.

)
 Model::updateOrCreate($attributes = array('key' => 'value'), $values = array('key' => 'value')); 

My question is how to edit the insertion fragment above so that I can use Model::updateOrCreate in it. My $attributes fields: "fk_workouts_id", "fk_users_id", "fk_exercises_id" and the rest in the $values that need to be changed.

I do not know how to implement this. Help Pls ...

+5
source share
2 answers

You may try,

First way

  $user = User::firstOrNew(array('name' => 'satish')); $user->field = 'value'; $user->save(); 

It will check for the user name=satish if it is found, and then return it. If not found, create a new one and return. Then you can set the field values.

Check out this link. http://laravel.com/docs/4.2/eloquent#insert-update-delete

The second way, Mannually Check record exists.

  $arr=array('field'=>"value"); $row = User::find($id); if ($row === null) { //Insert your record } else { //update your record } 

UPDATED

You can also use updateOrCreate - update an existing model or create a new model if it does not exist. updateOrCreate saved, so there is no need to call save() .

Example -

 // If there a flight from Oakland to San Diego, set the price to $99. // If no matching model exists, create one. $flight = App\Flight::updateOrCreate( ['departure' => 'Oakland', 'destination' => 'San Diego'], ['price' => 99] ); 
+6
source

I think you are working with a custom laravel framework, so you have the UpdateOrCreate method, because laravel does not provide such methods, if my assumption is correct, the code below can be useful for your application.

 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']); $attr_array = array('fk_workouts_id','fk_users_id','fk_exercises_id','section','status','item_index','updated_at'); foreach($exerciseData as $exerciseid){ $val_array = array($userId,$exerciseid,$exerciseValue['section_type'],1,$item_index,$workoutDetails['updated_at']); WorkoutExercise::updateOrCreate($attr_array,$val_array); } } 

But if you do not use such a custom framework than try with the code below, it may work fine

 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){ $item_index = $index+$kkey+1; // Retrieve the record by the fk_workouts_id, or create it if it doesn't exist... $exercise = WorkoutExercise::firstOrCreate($attributes = array('fk_workouts_id' => $id)); /* assign the values of other fields to store in database table*/ $exercise->fk_users_id = $userId; $exercise->fk_exercises_id = $exerciseid; $exercise->section = $exerciseValue['section_type']; $exercise->status = 1; $exercise->item_index = $item_index; $exercise->updated_at = $workoutDetails['updated_at']; $exercise->save(); // update the record } } 

good luck ... :)

+1
source

Source: https://habr.com/ru/post/1207596/


All Articles