我使用以下插入数组在 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);
上面的代码工作正常,并将数据数组插入数据库。
我已经学会了Model::updateOrCreate
的用法并成功地在我的一些模块中使用它(即)
Model::updateOrCreate($attributes = array('key' => 'value'), $values = array('key' => 'value'));
我的问题是如何编辑上面的插入代码段,以便我可以在其中使用Model::updateOrCreate
。我的$attributes
字段为'fk_workouts_id','fk_users_id','fk_exercises_id',其他字段为$values
,需要更改。
我不知道如何实现这一点。请帮忙......
答案 0 :(得分:8)
你可以试试,
第一种方式
$user = User::firstOrNew(array('name' => 'satish'));
$user->field = 'value';
$user->save();
如果找到,将检查用户name=satish
然后将其返回。如果没有找到,则创建新的并返回。然后你可以设置字段值。
检查此链接。 http://laravel.com/docs/4.2/eloquent#insert-update-delete
第二种方式 手动检查记录存在。
$arr=array('field'=>"value");
$row = User::find($id);
if ($row === null) {
//Insert your record
} else {
//update your record
}
更新
您还可以使用updateOrCreate
- 更新现有模型或创建新模型(如果不存在)。 updateOrCreate
会保留模型,因此无需拨打save()
。
示例 -
// If there's 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]
);
答案 1 :(得分:1)
我认为你正在使用laravel的自定义框架,所以你有UpdateOrCreate方法,因为如果我的假设是真的,laravel不提供这样的方法,而不是下面编写的代码可能对你的应用程序有用
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);
}
}
但是如果您没有使用这种自定义框架,请尝试使用以下代码,它可以正常工作
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
}
}
祝你好运...... :)