Advertisement
maprangsoft

การ insert update delete ซับซ้อน

Jun 2nd, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.66 KB | None | 0 0
  1.             $arrInsert = [];   
  2.             $arrDelete = [];
  3.  
  4.             if (!is_null($input['insert'])) {
  5.                 foreach ($input['insert'] as $value) { //loop งานที่ต้องบันทึก
  6.                     if ($value['type'] == 'area') { //ถ้าถูกเลือกพื้นที่มา
  7.                         //ดึงข้อมูลงานที่เลือกมา
  8.                         $workSelects = WorkSelect::where("area_id", $value['area_id'])->get();
  9.  
  10.                         foreach ($workSelects as $workSelect) {
  11.                             $arrInsert[] = [
  12.                                 'user_technician_id' => $value['user_technician_id'],
  13.                                 'type' => 'area',
  14.                                 'work_select_id' => $workSelect->select_id,
  15.                                 'area_id' => $workSelect->area_id,
  16.                                 'qty' => $workSelect->qty,
  17.                                 'price' => $workSelect->technician_price,
  18.                                 'user_created' => $user->id,
  19.                                 'created_at' => dateTimeNow(),
  20.                             ];
  21.                         }
  22.                     } elseif ($value['type'] == 'work') { //ถ้าถูกแตกงานมา
  23.  
  24.                         $arrInsert[] = [
  25.                             'user_technician_id' => $value['user_technician_id'],
  26.                             'type' => 'work',
  27.                             'work_select_id' => $value['work_select_id'],
  28.                             'area_id' => $value['area_id'],
  29.                             'qty' => $value['qty'],
  30.                             'price' => $value['price'],
  31.                             'user_created' => $user->id,
  32.                             'created_at' => dateTimeNow(),
  33.                         ];
  34.  
  35.                     } //end if
  36.                 }
  37.  
  38.                 if (isset($arrInsert)) { //ถ้ามีข้อมูล
  39.                     //บันทึกการแตกงาน
  40.                     WorkQuotationTechnician::insert($arrInsert);
  41.                 }
  42.             } //end if(!is_null($input['insert']))
  43.  
  44.             if (!is_null($input['update'])) {
  45.                 foreach ($input['update'] as $value) { //loop งานที่ต้องแก้ไข
  46.                     if ($value['type'] == 'area') { //ถ้าถูกเลือกพื้นที่มา
  47.                         foreach ($value['id'] as $workId) {
  48.                             $update = WorkQuotationTechnician::find($workId);
  49.                             $update->user_technician_id = $value['user_technician_id'];
  50.                             $update->save();
  51.                         }
  52.  
  53.                     } elseif ($value['type'] == 'work') { //ถ้าถูกแตกงานมา
  54.                         $update = WorkQuotationTechnician::find($value['id']);
  55.                         $update->user_technician_id = $value['user_technician_id'];
  56.                         $update->qty = $value['qty'];
  57.                         $update->price = $value['price'];
  58.                         $update->save();
  59.                     }
  60.                 }
  61.             } //end if(!is_null($input['update']))
  62.  
  63.             if (!is_null($input['delete'])) {
  64.                 foreach ($input['delete'] as $value) { //loop งานที่ต้องลบ
  65.                     $arrDelete[] = $value;
  66.                 }
  67.  
  68.                 if (isset($arrDelete)) {
  69.                     WorkQuotationTechnician::whereIn('id', $arrDelete)->delete();
  70.                 }
  71.             } //end if(!is_null($input['delete']))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement