如果条件保存laravel

时间:2016-11-08 10:49:56

标签: php laravel

我有两张桌子。 driver和part_time_available以相同的形式出现,当我选择driver type = parttime时,它会显示part_time_available字段(day,start_time,end_time)。

如果用户选择全时,如何制定条件。它没有将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之前,需要检查它的兼职与否。

任何想法?

1 个答案:

答案 0 :(得分:0)

你可以在整个foreach循环之前给出一个条件。如:

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