11.08.2017 Views

codebright

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Build An App 1: Playstation Game Collection 379<br />

1 public function edit(Game $game)<br />

2 {<br />

3 // Show the edit game form.<br />

4 return View::make('edit');<br />

5 }<br />

Our edit form needs to be able to populate the form with default items from our game, so we need<br />

to pass our game instance into the view. Let’s change this now.<br />

1 public function edit(Game $game)<br />

2 {<br />

3 // Show the edit game form.<br />

4 return View::make('edit', compact('game'));<br />

5 }<br />

We have used the compact() method once again to create the view data array. Now it’s time to<br />

handle form submission for the edit form. This is the handleEdit() action as it currently stands.<br />

1 public function handleEdit()<br />

2 {<br />

3 // Handle edit form submission.<br />

4 }<br />

Well that’s a little plain isn’t? I’m fairly sure that comment isn’t going to persist our edited game to<br />

the database. Let’s fill in the blanks. The edit form provides the game’s ID as a hidden field so we<br />

can use that to retrieve the game instance and modify its column values.<br />

1 public function handleEdit()<br />

2 {<br />

3 // Handle edit form submission.<br />

4 $game = Game::findOrFail(Input::get('id'));<br />

5 $game->title = Input::get('title');<br />

6 $game->publisher = Input::get('publisher');<br />

7 $game->complete = Input::has('complete');<br />

8 $game->save();<br />

9<br />

10 return Redirect::action('GamesController@index');<br />

11 }<br />

That’s better! We use the findOrFail() method on the Game class. It’s similar to the find() method,<br />

except that it will trigger an application 404 event if an object with the provided primary key cannot

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!