Laravel Error Message Undefined Variable in Blade View Template File

Posted on

In this article, another error triggered in a web-based application powered by Laravel framework will be discussed further. The error triggered is actually defined in the title of this article which is having an undefined variable. The variable which is undefined is located in the blade view template file. Below is the image of the error described :

As shown in the image above, there is an error specified concerning Undefined variable. Furthermore, the variable which is being undefined is also mentioned in the error display which the name of it is ‘data’.

The scenario of the above page execution can be described as follows :

1. User access a page with the ending trail of ‘/role’. It can be seen in the above image.

2. Based on the information given in no.1, trace the method and also the controller which is responsible for handling the process of the URL request. In the context of this article, it can be retrieved from the route file configuration which is usually represented with a file named web.php stored in a folder located in routes. Below is the location described in the form of a tree :

user@hostname:/var/www/html/laravel-project$ tree routes/
├── api.php
├── console.php
└── web.php

0 directories, 3 files
user@hostname:/var/www/html/laravel-project$ tree routes/

See the content inside the file named ‘web.php’ which is describing the URL mapping definition as shown below :


3. Based on the information given in no.2, check the method named ‘index’ in RoleController.php file for analyzing the process of rendering the blade view file template further. Below is the snippet code represent the content of the index method on the controller file mentioned :

public function index() {

$list_role = DB::table('role')->get()->toArray();

return view('role.index')->with('list_role', $list_role);


As it can be seen, the method returns a variable named ‘list_role’ to the blade view template file named index.blade.php which is located in the role folder. To be precisely described, below is the location of the file in the form of tree :

user@hostname:/var/www/html/laravel-project$ tree resources/views/role/
├── edit.blade.php
└── index.blade.php

0 directories, 2 files

4. Knowing that the method is passing a variable into a blade view template file named ‘index.blade.php’, check the content of the file which is shown as follows :

@foreach($list_role as $key => $role)
<td><?php echo $data->name ?></td>
<td><?php echo $data->description ?></td>

In the above snippet code, it is obvious that the iteration process of the variable named ‘$list_role’ is specified to print the name and the description of the role. But it is a mistake to use a variable name which is not defined as part of the iteration process as shown above. So, in order to solve the problem, the correct snippet code will be shown below :

@foreach($list_role as $key => $role)
<td><?php echo $role->name ?></td>
<td><?php echo $role->description ?></td>

So, the error for undefined variable can be vary from one with another. It depends on the variable name which hasn’t yet been defined in the blade view template file. In the case of this article, the variable name is $data which is directly called or summoned to print the value of attributes, the name and the description of a role. Since it hasn’t been defined yet in the iteration definition process, the variable is stated as undefined. To solve the problem, just use the variable which is included in the interation definition process which is stated in this part :

@foreach($list_role as $key => $role)

To summon the value of the attribute by printing it, just use the $role variable as shown in the above script.

One thought on “Laravel Error Message Undefined Variable in Blade View Template File

Leave a Reply