|

How To Pass Data To Views In Laravel

How To Pass Data To Views In Laravel

In this tutorial, we’ll start looking at how we can take variable data, and pass it to our views to be rendered in the Browser. This approach adheres to the Model View Controller architecture. In this most recent series of tutorials on Laravel, we have not yet talked about Models that typically deal with retrieving data, but we have covered the basics of Routing and Views. This is enough knowledge for us to now start the process of adding dynamic data to our routing and views, and observing how this results in being output to the browser.


The Goal: List Some Video Games

Right now, we have enough knowledge from the basics we have learned so far to set up a route, and load a view. How about we set up a route such that when we visit /games, we will see a list of games. Here is a route, and view file that will allow this. We will start by doing this statically.


/routes/web.php


/resources/views/games.blade.php


http://54.dev/games

display list of games


Make the data dynamic

Just up above, we got a basic web page to display a few video games when visiting the /games route. Let’s now remove the hard coded values from the view file, and instead put the names of the games in the routes file. Here is a modified version of what we had above. The data will now be dynamic.


/routes/web.php


/resources/views/games.blade.php


http://54.dev/games

display list of games

As we can see, the data is now dynamic. It is being passed as an array from the routes file to the view for rendering. It’s important to note that it is the second argument passed to the view() function which holds the dynamic data. That second argument is to be an array. Now, the keys of that array become the variable names in the view. So in our example here, that second argument is an array which has only one key which is games. That games key itself holds an array of the three games we wish to pass to the view. Then, in the view, we use a standard PHP foreach construct to loop over each game and output the result. Need to see proof that this is dynamic? Let’s go ahead and update the array of data we are passing and then observe the result in the browser. (The view file is unchanged at this point).


/routes/web.php


http://54.dev/games

new dynamic data


The array of many formats

This array that gets passed as the second argument can come in many different forms. So far we have done the inline version so to speak. This is where we populate the array right in place. This works, but can get a little cumbersome as the data set that you want to pass grows. Let’s look at a few different ways to do the same thing so to speak. The following snippets all do the same exact thing.


Inline


Setting up a data array


The with() method


The compact() function


Passing Multiple Arrays of Data at Once

One nice way to do things is to simply assume your return statement in your specific route will look like this:

Since the $data variable is an array, it can contain as many keys as you may ever need to store data. So let’s consider that not only do we want to list the Games, but also the Publishers of those games as well. How might we do that? Let’s see!


/routes/web.php


/resources/views/games.blade.php


http://54.dev/games

passing multiple arrays of data to the view

Fantastic! Maybe you also want to include the Year the game was released as well. It’s very easy to scale this up, and all you ever have is that same $data variable as the second argument to view().


/routes/web.php


/resources/views/games.blade.php


http://54.dev/games

passing more and more data to the view mvc

Of course you can use whichever approach works best for you. We all learn differently, and surely some of us will like one approach and others will take a different approach. It all depends on what makes the most sense in your own mind. Always try to make it easy on yourself.


Make Use of Blade

In the examples so far, we used standard PHP to present the data to the browser in the view files. What if I told you there is a much easier way to do this? Well, I’m telling you, there is and it is called Blade. Let’s see how we might make use of blade to allow for an easier to type syntax.

Take this code that outputs an unordered list, and make it a little easier to type and reason about.

If we take the table we rendered earlier, we can also update that to use blade.


How To Pass Data To Views In Laravel Summary

When we first began we were simply working our way through setting up a route, and then rendering a view. In this tutorial, we took things a step further and looked at how you can pass dynamic data to your views. In our routes file, we saw that we make use of the view() function. It’s first argument is a string which specifies which view file to load. The second argument to view() is an array which holds dynamic data that we can pass to the view file. We took a close look at how to use this array, as well as access the data in our view files using both native PHP and Laravel’s Blade templating engine.