/** * Update a comicbook series * PUT /content/{title} * * @param int $title * @return Response */ public function update($title) { //Set rules $rules = array('book_name' => 'required|min:1', 'publisher_name' => 'required|min:1', 'book_description' => 'max:2000', 'genres' => 'min:1', 'characters' => 'min:1'); //Validate $validator = Validator::make(Input::all(), $rules); //Instance of Comicbook model $comic = new Comicbooks(); $book_id = $comic->series($title)->select('comicdb_books.id')->first(); //If the comicbook series exists if (!is_null($book_id)) { //If validation passes if ($validator->passes()) { //Instance of Publisher model $publishers = new Publishers(); //Set variables $book_name = strtolower(Input::get('book_name')); $publisher = strtolower(Input::get('publisher_name')); //If publisher already exists, get the id of that publisher from the comicdb_publishers table $publisherExists = $publishers->where('publisher_name', $publisher)->select('id')->first(); if (isset($publisherExists->id)) { $publisher_id = $publisherExists->id; } else { $publisher_id = $publishers->insertGetId(array('publisher_name' => $publisher)); } //Update comic series $update_comic = $comic->findOrFail($book_id->id); $update_comic->book_name = $book_name; $update_comic->book_description = Input::get('book_description'); $update_comic->publisher_id_FK = $publisher_id; $update_comic->save(); //Delete then reinsert all the values because that way is easier. DB::table('comicdb_genrebook')->where('book_id_FK', $update_comic->id)->delete(); foreach (Input::get('genres') as $key => $genre) { DB::table('comicdb_genrebook')->insert(array('book_id_FK' => $update_comic->id, 'genre_id_FK' => $genre)); } //Add issue character information into the comicdb_character table and keys into the comicdb_characterbook table DB::table('comicdb_characterbook')->where('book_id_FK', $update_comic->id)->delete(); foreach (Input::get('characters') as $key => $character) { $character_id = Characters::insertGetId(array('character_name' => $character)); DB::table('comicdb_characterbook')->insert(array('book_id_FK' => $update_comic->id, 'character_id_FK' => $character_id)); } return Redirect::to('browse')->with('postMsg', 'The book has been updated!'); } else { return Redirect::to('content.series.edit')->with('postMsg', 'Whoops! Looks like you got some errors.')->withErrors($validator)->withInput(); } } else { return Redirect::to('content.series.edit')->with('postMsg', 'That book does not exist!'); } }