예제 #1
0
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update($id, Request $request)
 {
     $book = \App\Book::findOrFail($id);
     // $book->update(\Input::all());
     // return \Redirect::back()->with('message','Book updated.');
     // if($book){
     // $rules = \App\Book::$rules;
     //$rules['name'] = 'required|min:2';
     // $validator = \Validator::make(\Input::all(), $rules);
     // if($validator->passes())
     // {
     $book->description = $request->description;
     $book->publisher_id = \App\Publisher::firstOrCreate(['name' => ucfirst($request->publisher)])->id;
     $book->category_id = \App\Category::firstOrCreate(['name' => ucfirst($request->category)])->id;
     $book->language_id = \App\Language::firstOrCreate(['name' => ucfirst($request->language)])->id;
     $book->release_date = $request->release_date;
     $book->save();
     //going over author names build up array of author ids by getting author ids and creating author if does not exist
     $authorIds = [];
     $authors = $request->authors;
     if ($authors) {
         foreach ($authors as $author) {
             $authorIds[] = App\Author::firstOrCreate(['name' => ucfirst($author)])->id;
         }
         //atached book to authors
         $book->authors()->sync($authorIds);
         // $book->authors()->attach($authorIds);
     }
     if ($request->hasFile('image')) {
         $this->setBookImage($book, $request);
     }
     flash('Book updated.');
     return \Redirect::back();
     //   }
     //   return \Redirect::back()
     //     //->with('message','There were some errors. Please try again later..')
     //     ->withInput()
     //     ->withErrors($validator);
     //   }
     //
     //   flash()->error('Book does not exist.');
     // return \Redirect::back();
 }