2016-11-08 79 views
0

我有2個表。驅動程序和part_time_available在相同的表單中,當我選擇driver type = parttime時,它將顯示part_time_available字段(day,start_time,end_time)。如果保存條件laravel

如何讓條件,如果用戶選擇全職。它沒有將part_time_available字段存儲到數據庫。

這裏是到目前爲止我savehandler代碼:

public function saveHandler(Request $request, $obj) 
{ 
    try { 
     DB::beginTransaction(); 
      $obj->fill($request->all()); 
      if (!$obj->save()) { 
       throw new ValidationException($obj->errors()); 
      } 
      foreach($request->parttimeAvailabilities as $pta) { 
       \Log::info($pta); 
       if (empty($pta['id'])) { 
        $parttimeAvailability = new PartTimeAvailability(); 
       } 
       else { 
        $parttimeAvailability = PartTimeAvailability::find($pta['id']); 
       } 
       $parttimeAvailability->driver()->associate($obj); 
       $pta['driver_id'] = isset($pta['driver_id']); 
       $parttimeAvailability->day = $pta['day']; 
       $parttimeAvailability->start_time = isset($pta['start_time']) ? $pta['start_time'] : '00:00:00'; 
       $parttimeAvailability->end_time = isset($pta['end_time']) ? $pta['end_time'] : '00:00:00'; 
       $parttimeAvailability->available = isset($pta['available']); 
       $parttimeAvailability->save(); 
      }; 
      $obj->save(); 
      if (!$parttimeAvailability->save()) { 
       throw new ValidationException($parttimeAvailability->errors()); 
      } 
     DB::commit(); 
     return $this->sendSuccessResponse($request); 
    } catch (ValidationException $e) { 
     DB::rollback(); 
     \Log::error($e->errors); 
     return $this->sendErrorResponse($request, $e->errors); 
    } catch (Exception $e) { 
     DB::rollback(); 
     \Log::error($e->getMessage()); 
     return $this->sendErrorResponse($request,'Unable to process. Please contact system Administrator'); 
    } 

} 

我的意思是運行的foreach之前,需要檢查它的兼職與否。

有什麼想法嗎?

回答

0

您可以在整個foreach循環之前給出一個條件。如:

if($request->get('driver_type') != 'full_time'){ 
foreach loop 
}