Table of Contents

Laravel Views

Views

A zombie developer entangles logic and HTML elements in the same page, whereas a Laravel developer separates their views from their logic.

Zombies are considered to be messy and not too friendly because they write code all over the place. As a Laravel developer we separate our data output from our data logic. In this section, we're going to talk about using views. Leveraging the powerful features of Laravel will help us gain a clear VIEW of what we want to build (sorry for the cheesy pun).


Views

Views are essentially the HTML and the layout elements of a page. A view is what our user will see when they visit a page.

So, how might we go about creating a new VIEW for a specified route? Glad you asked, as it's very straightforward. Back in section 2, we created a simple route inside of routes\web.php called 'graveyard' that printed out a string, which looked like this:

<?php

Route::get('graveyard', function(){
    echo 'Welcome to the graveyard!';
});

If we wanted to implement the functionality above with a view, our code would look like this:

<?php

Route::get('graveyard', function(){
    return view('graveyard');
});

And this will load the file located at resources/views/graveyard.php. So, if we created that file and added the following code to this file:

<html>
<head>
    <title>Welcome!</title>
</head>
<body>

    <p>Welcome to the graveyard!</p>

</body>
</html>

We would get the same functionality as the first view, except when using the view we would add valid HTML to our page, and this would be the correct way of structuring our code and displaying data on the screen.

All of your view files will be located inside of the resources/views/directory. Keeping our files well organized and in proper locations will help us keep our sanity and will make our lives much easier.

Now let's say that we wanted to get all the zombies from our database and pass them to our view, how might we do this. Simple enough, our route would look like this:

<?php

use App\Zombie as Zombie;

Route::get('zombies', function(){
    $data = array('zombies' => Zombie::all());
    return view('zombies', $data);
});

And that would pass all our zombies to a view called zombies.php located in resources/views/ folder. After creating a new file at resources/views/zombies.php we could list out our zombies by adding the following code:

<html>
<head>
    <title>Zombies</title>
</head>
<body>

    <ul>
        <?php foreach($zombies as $zombie): ?>
            <li><?php echo $zombie->name; ?></li>
        <?php endforeach; ?>
    </ul>

</body>
</html>

If we were to open up a web browser and navigate to site.com/zombies we would get an unordered list of our zombie names, which would look like the following:

To pass data to your views, you can just include all of the variables inside of an array that gets passed as a second argument in the view function.

return view('zombies', ['zombies' => Zombie::all()]);

Using views in Laravel is super easy, and it gets even easier if we choose to use the built-in blade templating engine. What is the blade templating engine? Let's discuss that next.