I'm using the following Laravel 5.4 transaction update in the controller's action:
DB::transaction(function () use ($job,$now){
$nowDate = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $now);
/* DB::table('cavity_jobs')->where(['job_id' => $job->id])->update(['status' => 0]);
DB::table('cavity_actions')->where(['job_id' => $job->id])->whereNull('closed')->update(['closed' => $now]);
DB::table('cavity_stops')->where(['job_id' => $job->id])->whereNull('closed')->update(['closed' => $now]);
DB::table('eqtype_jobs')->where(['job_id' => $job->id])->update(['status' => 0]);
foreach ($job->sets as $set){
DB::table('sets')->where(['id' => $set->id])->update(['in_job' => 0, 'updated_by' => Auth::user()->id]);
}*/
foreach ($job->currentEquipments as $eq){
$diffInMinutes = $eq->pivot->created_at->diffInMinutes($nowDate);
$op = \App\Operation::find($eq->pivot->aid);
$dd= DB::table('equipments')->where(['id' => $eq->id])->update(['approved' => 0,'working_time' => ($diffInMinutes+$eq->working_time), 'production' => $this->getEqProduction($eq,$op), 'updated_at' => $now ,'updated_by' => Auth::user()->id]);
dd($dd,$eq->id);
}
// DB::table('jobs')->where(['id' => $job->id])->update(['completed' => $now, 'updated_by' => Auth::user()->id]);
});
All the commented update statements seems to work fine. However, the uncommented statement, through the foreach loop does not work. i.e the equipments table remains as it is.
In order to debug the issue, I had dd($job->currentEquipments) and it prints out the required array of objects needed in the foreach, so the foreach loop works. Then as shown in the code, I printed out the output of the update method through the $dd and also the $eq->id. The strange thing here, the $dd prints out 1 with no any errors, so the update method works fine with out any problem, as supposed from the value 1 returned, and also I have got the correct $eq->id.
I have got missed and I don't know what's the problem?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire