Laravel Error Message : Undefined variable: variable_name

Posted on

This article is an article where an error as stated in the title of this article happened. The title which is also describing the error message is ‘Undefined variable :’. Depends on the circumstance and the condition regarding the script executed, the error message can be attached with various variable name. So, in the title of this article, the error which is described as an error message is specified as ‘Undefined variable : variable_name’.

The variable_name can be anything depend on the error which is pointed out after executing script which is becoming the cause of the error.

ErrorException "/var/www/html/laravel-project/storage/framework/views/7f3e2b293958d95a015993a2402e50518101efa8.php line 50" Undefined variable: list_server (View: /var/www/html/laravel-project/resources/views/server/index.blade.php)

In the above error message, it is actually retrieved from a firebug plugin addons installed in Mozilla Firefox web browser. The error involving error the failure for not assigning value for the variable. This case happens when a file defined in a blade view template view there is a variable named list_server in this article context has failed to be set. Below is the content of the blade view template file :

@foreach ($list_server as $key => $server){{ ++$i }}
...
@endforeach

The error triggered when a method inside controller is executed but it is not initializing the variable needed in the blade view template. It is not generating variable named $list_server and then pass it to the blade view template file as shown below :

 public function saveData(Request $request) {
        ...
        return('server.index');
 }

The error can be solved, if only the method above is modified so that it will pass a variable to the blade view file template as shown below :

 public function saveData(Request $request) {
        ...
        $list_server = Server::all();
        return view('server.index')->with('list_server',$list_server);
 }

Definitely the error above will be handled since the variable named list_server which is defined in the blade view template file is perfectly assigned from the controller file where it is clearly shown in the above line of code, especially in this part :

 return view('server.index')->with('list_server',$list_server);

 

Leave a Reply