How to Pass Data to View in Laravel?

Last updated on February 28, 2023

In Laravel, we pass data into view from the controller and then show it using HTML elements. This data comes from databases, APIs, and sometimes it is static.

How does Laravel call view? let’s understand it.

Laravel has a directory specifically for views under the resources folder and it has included a power templating engine called The Blade. So each file name ends with .blade.php.

Usually, the view comes from the method. So whenever a method has to call view it uses Laravel’s built-in function view(). All such methods must have routes. Laravel keeps the route folder on the root, and it has web.php which deals with the web interface.

So when we call URL in the browser, Laravel goes to web.php for the route that points to a method. Then it runs all scripts and returns the view.

Get Started

Let’s create a view file with the name user-list.blade.php inside the resources/views directory. Then create a user controller by the following the command,

php artisan make:controller UserController

Pass an Array to View

Create list() method with the following static array which we will be passing into view.

public function list()
{
    $users = [
        [
            'id' => 1,
            'name' => 'John',
            'age' => 30
        ],
        [
            'id' => 2,
            'name' => 'Smith',
            'age' => 20
        ]
    ];
}

After this,  we need to pass this static array to the view, for that we need to use Laravel’s built-in function view(). Add the following script at the end of list() method,

return view('user-list', ['users' => $users]);

Here, we have passed two arguments in the view() function which are file name and data in array format. Data would be accessible through $users variable in the view file.

Next, needs to create a route for list() method, so inside the route directory add the following script in the web.php,

Route::get('/user-list', [UserController::class, 'list']);

We can access data in the view, to check out the data add the following script in the view file

{{ dd($users); }}

In Laravel, double curly braces escape HTML elements as well as echo the variable. But here we have an array, so we use dd() function which means die and dump.

Pass String and Integer to View

Laravel allows passing string and integer or any single value. Let’s create a todaysDate() method that will be passing string or integer to view.

public function todaysDate()
{
    $date = '25 May 2022';

    return view('todays-date', compact('date'));
}

This method passes a string to view file (todays-date.blade.php) through PHP built-in function compact(). To print the string in the view file (todays-date.blade.php) we will be using double curly braces, below is an example,

{{ $date; }}

We can use helper functions inside blade files, here are some helpful functions from Laravel helper. Now we need a route that would run todaysDate() method into the browser.

Route::get('/todays-date', [UserController::class, 'todaysDate']);

Now we can check the result in the browser by running the following command on the terminal,

php artisan serve

This command runs the Laravel application on the development server. Following URL would print string into the browser,

http://127.0.0.1:8000/todays-date

Conclusion

To pass data to view in Laravel, you need to use the view() function. It accepts two parameters, first is the name of the blade file and second is data that needed be to pass into the view or blade file.


Written by
I am a skilled full-stack developer with extensive experience in creating and deploying large and small-scale applications. My expertise spans front-end and back-end technologies, along with database management and server-side programming.

Share on:

Related Posts

Tags: Laravel,