Laravel

Laravel Views with Example

January 30, 2018

author:

Laravel Views with Example

As we discussed earlier in Laravel Introduction and Controllers, Laravel framework promotes MVC architecture. Laravel View is a component of MVC View. It helps us separate the presentation logic from application logic. Views are usually stored in resources/views/ folder. They contain HTML served by the application. Thus, views are a combination of PHP (Blade) and HTML.

# Blade Setup

Let us now create a simple blade file in resource/views directory to render view:

# resources/views/about.blade.php

<html>
    <body>
        <h1>This is About Page.</h1>
        <p>Laravel is awesome!</p>
    </body>
</html>

# Redirect to Blade View

Next, to render its effect in the browser, we need to register this view with our controller. So let us go back to web.php and wire this fresh view.

# routes/web.php
...
Route::get('about', function () {
    return view('about');
});

And thus we finally displayed data on the browser screen.

localhost:8000/about
Laravel Route Overview

# Passing Data to Views

This is the most common yet important element of any framework. Laravel has multiple ways of doing so. Let us try and cover most of them one after the another.

Add the following crux of data to your project web.php file:

# routes/web.php
...
Route::get('about', function () {
    //replace with
    return view('about', [
        'language' => 'Laravel'
    ]);
});

Then, to use this passed data (language here), let us fetch it from a view as:

# resources/views/about.blade.php

<html>
    <body>
        <h1>This is About Page.</h1>
        <p><?= $language ?> is awesome!</p>
    </body>
</html>

As seen in web.php, in about route, the first argument stores name of the view file and second one holds an array of data. All we have done is pass an array with a key language with the about view. And in about.blade.php, we have referenced the same key to fetch data. Now, let’s test this in a browser.

http://localhost:8000/about
Laravel Blade View Example

# Using with() to pass data

The other way to pass data from routes is to use a helper function with instead of creating an array. with() behaves similar to an array and expects data in a key-value pair. Here’s how we can use it:

# routes/web.php
...
Route::get('about', function () {
    //replace with
    return view('about')->with('language', 'Laravel');
});

You can leave the about.blade.php as it is for the time being and test in browser. You’ll notice no change in the resultant output.

# Using compact() to pass data

The above two methods work perfectly well, but the issue arises when we have a long list of data to be passed from routes. In such cases, we would have to create a long list of arrays and attach them to view. It does become cumbersome. Here’s a way out – use compact().

# routes/web.php
...
Route::get('about', function () {
    //replace with
    $language = 'Laravel';
    return view('about', compact('language'));
});

Replace the earlier code with this one and test in the browser for yourself. It works the same way as other two. But this is simpler and cleaner than them, right?

Conclusion:

In this post we got hands on various laravel view which is nothing but a blade file. We also learned how to pass data from route and controller to the view with its various methods. Hope it was a smooth run for you.

Questions & Comments:

Thank you for reading. If you have any questions or confusion in Laravel Blade Views or how to pass data to views from controller, share them in the comment section below.

Leave a comment

Your email address will not be published. Required fields are marked *