public function run() { $faker = Faker::create(); foreach (range(1, 20) as $index) { $title = $faker->sentence($nbWords = 6); $course = Course::create(['title' => $title, 'instructor' => $faker->name, 'image_url' => 'images/assets/2014/5.jpg', 'address' => '12 meses', 'course_level' => $faker->randomElement(['profesional', 'tecnico', 'curso']), 'body' => $faker->text($maxNbChars = 200), 'slug' => \Str::slug($title), 'available' => true]); foreach (range(1, 5) as $index) { Download::create(['file_type' => $faker->randomElement(['material', 'modulos', 'horario']), 'description' => $faker->sentence($nbWords = 7), 'file_url' => 'data/courses/1.pdf', 'course_id' => $course->id]); } } }
public function downloadStore($id) { $file = Input::file('file_url'); $data = Input::only(['file_type', 'description', 'file_url']); $rules = ['file_type' => 'required', 'description' => 'required', 'file_url' => 'required']; $validation = Validator::make($data, $rules); if ($validation->fails()) { return Redirect::back()->withInput()->withErrors($validation->messages()); } else { $getRealPath = 'data/courses/'; $download = new Download($data); $download->course_id = $id; $download->file_url = $getRealPath . $data['file_url']->getClientOriginalName(); $download->save(); if ($download->save()) { $i = 0; //separamos el nombre de la img y la extensión $info = explode(".", $file->getClientOriginalName()); //asignamos de nuevo el nombre de la imagen completo $newFile = $file->getClientOriginalName(); //mientras el archivo exista iteramos y aumentamos i while (file_exists($getRealPath . $newFile)) { $i++; $newFile = $info[0] . "(" . $i . ")" . "." . $info[1]; } //guardamos la imagen con otro nombre ej foto(1).jpg || foto(2).jpg etc $file->move($getRealPath, $newFile); //si ha cambiado el nombre de la foto por el original actualizamos el campo foto de la bd if ($newFile != $download->file_url) { $download->file_url = $getRealPath . $newFile; $download->save(); } } } return Redirect::back()->with(['confirm' => 'Se inserto el registro correctamente.']); }