/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $input = $request->all();
     $rules = array('photo' => 'mimes:jpeg,jpg,bmp,png');
     $validation = Validator::make($input, $rules);
     if ($validation->fails()) {
         return response()->json($validation->errors->first(), 400);
     }
     $destinationPath = 'uploads/photos';
     $extension = $request->file('photo')->getClientOriginalExtension();
     $fileName = rand(11111, 99999) . '.' . $extension;
     $uploadSuccess = $request->file('photo')->move($destinationPath, $fileName);
     if ($uploadSuccess) {
         // Agregamos la nueva foto a la base de datos
         $photo = new Photo();
         $photo->name = $fileName;
         $photo->imageable_id = 0;
         $photo->imageable_type = "Adn\\Vehicle";
         $photo->save();
         return response()->json(['id' => $photo->id, 'name' => $photo->name, 'success' => 200]);
     } else {
         return response()->json('error', 400);
     }
 }
 /**
  * Cargamos los datos del nuevo vehículo.
  */
 public function postCreate(Request $request)
 {
     // Recuperamos el ID del usuario
     $userId = $request->user()->id;
     // Declaramos las reglas de validación
     $validator = Validator::make($request->all(), ['state' => 'required', 'city' => 'required', 'year' => 'required|integer', 'mark' => 'required', 'model' => 'required', 'fuel' => 'required', 'transmission' => 'required', 'doors' => 'required', 'color' => 'required', 'price' => 'required', 'photos' => 'required']);
     // Validamos los datos
     if ($validator->fails()) {
         return redirect('/account/vehicles/create?type=' . $request->vehicle_type)->withInput()->withErrors($validator);
     }
     // Agregamo los detalles del vehiculos.
     $carVan = new CarVan();
     $carVan->kilometers = $request->kilometers;
     $carVan->save();
     // Creamos el vehiculo
     $vehicle = new Vehicle();
     $vehicle->user_id = $userId;
     $vehicle->state_id = $request->state;
     $vehicle->mark_id = $request->mark;
     $vehicle->city_id = $request->city;
     $vehicle->model_id = $request->model;
     $vehicle->car_van_id = $carVan->id;
     $vehicle->fuel_id = $request->fuel;
     $vehicle->transmission_id = $request->transmission;
     $vehicle->door_id = $request->doors;
     $vehicle->color_id = $request->color;
     $vehicle->year = $request->year;
     $vehicle->price = $request->price;
     $vehicle->save();
     // Asiganmos el id del vehículos a  las fotos
     $photos = $request->photos;
     $totalPhotos = count($photos);
     if ($totalPhotos > 0) {
         for ($i = 0; $i < $totalPhotos; $i++) {
             $photo = Photo::find($photos[$i]);
             $photo->imageable_id = $vehicle->id;
             $photo->save();
         }
     }
     // Redireccionamos
     return redirect('/account/vehicles')->with('message', trans('Su vehículo se ha publicado correctamente.'));
 }