Download The Laravel Survival Guide - Tony Lea PDF

TitleThe Laravel Survival Guide - Tony Lea
TagsPhp Databases Installation (Computer Programs) Computer File Information Technology Management
File Size3.2 MB
Total Pages98
Table of Contents
Chapter 1 - Getting Started
Chapter 2 - Composer & The Laravel Installer
Chapter 3 - The Laravel Structure
Chapter 4 - Routing
Chapter 5 - Models
Chapter 6 - Model Relationships
Chapter 7 - Mutators
Chapter 8 - Views
Chapter 9 - Blade
Chapter 10 - Controllers
Chapter 11 - Piecing It Together
Chapter 12 - Artisan
Chapter 13 - Middleware
Chapter 14 - Authentication
Chapter 15 - Requests
Chapter 16 - Responses
Chapter 17 - Migrations
Chapter 18 - Seeds
Chapter 19 - Security
Chapter 20 - Testing
Chapter 21 - Wrapping Up
Document Text Contents
Page 2

The Laravel Survival Guide

Tony Lea

Page 49

3 $zombies = Zombie::all();

4 }

Great, we’ve stored all our zombies from our zombie model inside of the $zombies

Before we go to the next step, I want to point out that our app will not know where to find

If you go back to chapter 5 where we created this zombie model we put it inside of the App
namespace since it is in the app folder, so we would need to call App\Zombie::all(). Or
we could just tell our controller which zombie to use, like so:

1 use App\Zombie as Zombie;

And now, Altogether our controller would look like this:

1 php


3 namespace App Http Controllers


5 use App Http Controllers Controller

6 use App Zombie as Zombie


8 class ZombieController extends Controller


10 public function show

11 Show our zombies

12 $zombies Zombie::all



4. Pass our zombie data to our view

Passing our zombie data to our views is very straightforward. Inside of the show()
function, we need to load a view file and pass along the zombie data. To load a view file
we can return a view function at the end of our method like so:

1 return view('zombies', $data);

The first argument is a string with the view we want to load (located at re
sources\views\zombies.blade.php) and the second argument is an array of data that we
wish to pass to our view.

So, all together we would want our show() function from our controller to look like the

1 public function show(){


3 $zombies = Zombie::all();



6 $data = array('zombies' => $zombies);



Page 50

9 return view('zombies', $data);

10 }

Simple enough, now inside of our resources\views\zombies.blade.php we will have an
array of zombies stored in a variable called $zombies.

Notice that if we were to create a data array that looked like

$data = array('zombie guys' => $zombies) we would then have an array of zombies available in a
variable called $zombie_guys.

The last thing we need to do is to display our zombies in our views.

Inside of our zombie view file located at resources\views\zombies.blade.php we can
add a basic HTML page with an unordered list and loop through each of our zombies like

1 <html>

2 <head>

3 <title>Zombies</title>

4 </head>

5 <body>


7 <ul>

8 @foreach($zombies as $zombie)

9 <li>{{ $zombie->name; }}</li>

10 @endforeach

11 </ul>


13 </body>

14 </html>

In the example above we do a simple foreach statement and loop through each of the
zombies and list out their name.

Also, notice that above we are using blade syntax that we covered previously.

There you go! That was a fundamental overview of how each piece will work from your
route, model, controller, and view.

Pretty fun stuff, right!

Now that we have a basic overview of how our app works let’s move on to talking about
an excellent helper tool in Laravel called artisan that will make our lives much easier.

Page 97

Words of Encouragement

Page 98

A New Reality

Similer Documents