コード例 #1
0
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $rules = array('plate_number' => 'required|unique:vehicles', 'vehicle_type_id' => 'required', 'license_type_id' => 'required');
     $messages = ['plate_number.required' => trans('vehicle.plate_number_error_message'), 'vehicle_type_id.required' => trans('vehicle.vehicle_type_error_message'), 'license_type_id.required' => trans('vehicle.license_type_error_message')];
     $validator = Validator::make(Input::all(), $rules, $messages);
     if ($validator->fails()) {
         return redirect()->back()->withErrors($validator)->withInput();
     }
     $license_types = $request->license_type_id;
     if (empty($license_types[0])) {
         return redirect()->back()->with('error', 'Atleast select one license type');
     }
     $model = Vehicle::create($request->all());
     foreach ($license_types as $license_type) {
         $model1 = new LicenseTypeVehicle();
         $model1->license_type_id = $license_type;
         $model1->vehicle_id = $model->id;
         $model1->save();
     }
     Session::flash('flash_message', trans('vehicle.vehicle_on_create'));
     return Redirect::to('/vehicles');
 }