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 372<br />

1 public function index()<br />

2 {<br />

3 // Show a listing of games.<br />

4 $games = Game::all();<br />

5 return View::make('index', compact('games'));<br />

6 }<br />

In our index() action we use Game::all() to retrieve a collection of all of the games in the system.<br />

Then we use compact() and View::make() to pass the collection into our index view. If you haven’t<br />

used compact() before, it simply creates an array out of a number of variables, using their names<br />

as array keys. It’s the exact opposite of the extract() PHP function. I like to use compact() when I<br />

only need to pass a small number of values to my views.<br />

Now that we have a complete collection of our games, why not create a view to display them? Here’s<br />

the contents of app/views/index.blade.php.<br />

1 @extends('layout')<br />

2<br />

3 @section('content')<br />

4 <br />

5 All Games Gotta catch 'em all!<br />

6 <br />

7<br />

8 <br />

9 <br />

10 Create Game<br />

12 <br />

13 <br />

14<br />

15 @if ($games->isEmpty())<br />

16 There are no games! :(<br />

17 @else<br />

18 <br />

19 <br />

20 <br />

21 Title<br />

22 Publisher<br />

23 Complete<br />

24 Actions<br />

25 <br />

26 <br />

27

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

Saved successfully!

Ooh no, something went wrong!