public function run() { $faker = Faker::create(); foreach (range(1, 10) as $index) { Genre::create(['type' => $faker->locale]); } }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { if ($request->ajax()) { Genre::create($request->all()); return response()->json(["mensaje" => "creado"]); } }
/** * Execute the console command. * * @return mixed */ public function handle() { $url = "http://omdbapi.com/?i="; // get all the videos with an IMDB ID $videos = Video::whereNotNull('imdb')->get(); echo "\nUpdating....\n\n"; // loop through all the videos foreach ($videos as $video) { // get JSON data $json = file_get_contents($url . $video->imdb); $obj = json_decode($json); // update fields of the video with JSON data $video->product->name = $obj->Title; $video->poster = '/img/posters/' . $video->imdb . '.jpg'; $video->release_year = $obj->Year; if ($obj->Released !== 'N/A') { $video->release_date = new \DateTime($obj->Released); } // download the poster if (!file_exists('/img/posters/' . $video->imdb . '.jpg') && filter_var($obj->Poster, FILTER_VALIDATE_URL) !== false) { $buffer = file_get_contents($obj->Poster); $file = fopen(public_path() . '/img/posters/' . $video->imdb . '.jpg', 'w+'); fwrite($file, $buffer); fclose($file); } // save the video $video->save(); // delete the products gnere DB::table('genre_product')->where('product_id', $video->product_id)->delete(); // get the genres $genres = explode(',', $obj->Genre); foreach ($genres as $key => $genre) { $genres[$key] = trim($genre); } // add the genres foreach ($genres as $genre) { // if genre does not exist create it $record = Genre::where('name', $genre)->first(); if ($record === null) { $record = Genre::create(['name' => $genre]); } // attatch genre to video $video->product->genres()->attach([$record->id]); } // log to screen echo $video->product_id . ". \t" . $video->product->name . " updated successfully.\n"; } }
/** * @param array $data * @return mixed */ public function create(array $data) { return Genre::create($data); }
public function addGenre(Request $request) { $input = $request->all(); Genre::create($input); return redirect()->back(); }
public function StoreGame(Request $request, $info) { // get the genres $genres = str_getcsv($request->genre); foreach ($genres as $key => $genre) { $genres[$key] = trim($genre); } // create product record $product = new Product($request->all()); // use database transaction to save the records DB::transaction(function () use($product, $info, $genres) { // save the product record $product->save(); // set the product id of the details record $info->product_id = $product->id; if (filter_var($info->poster, FILTER_VALIDATE_URL) !== false) { // download the poster if (!file_exists('/img/games/' . $info->product_id . '.jpg')) { $buffer = file_get_contents($info->poster); $file = fopen(public_path() . '/img/games/' . $info->product_id . '.jpg', 'w'); fwrite($file, $buffer); fclose($file); } // set the poster url $info->poster = '/img/games/' . $info->product_id . '.jpg'; } // save the details record $info->save(); // add the genres foreach ($genres as $genre) { // if genre does not exist create it $record = Genre::where('name', $genre)->first(); if ($record === null) { $record = Genre::create(['name' => $genre]); $product->genres()->attach([$record->id]); } else { $product->genres()->attach([$record->id]); } } }); }
private function checkGenres($genres) { $user = Auth::user(); $currentGenres = array_filter($genres, 'is_numeric'); $newGenres = array_diff($genres, $currentGenres); foreach ($newGenres as $newGenre) { $genre = Genre::create(['name' => $newGenre]); $user->genres()->save($genre); $currentGenres[] = $genre->id; } return $currentGenres; }