Laravel Error Message : Creating default object from empty value

Posted on

This is an article where the main purpose to write the article itself is to discuss further on how to handle the error stated as in the title of this article. The error given is an impact from executing a script of web-based application powered by Laravel framework. The following explanation will use a certain file and certain snippet code for an example :

The error as stated as an ErrorException is located as follows :

Creating default object from empty value

It is actually exist and it is pointed out in a controller file named ServerController.php in line 171 as shown in the following error message located in app/Http/Controllers folder :

ErrorException in /var/www/html/laravel-project/app/Http/Controllers/ServerController.php line 171 Creating default object from empty value

The error above is retrieved from firebug tool installed as a plugins in Mozilla Firefox Web browser. And actually, the error itself  can also be shown or viewed in the following log file named laravel.log normally located in folder storage/log :

[2017-05-08 09:33:27] local.ERROR: ErrorException: Creating default object from empty value in /var/www/html/laravel-project/app/Http/Controllers/ServerController.php:171
Stack trace:
#0 /var/www/html/laravel-project/app/Http/Controllers/ServerController.php(171): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'Creating defaul...', '/var/www/html/i...', 171, Array)
#1 [internal function]: App\Http\Controllers\ServerController->checkGeneral(Object(Illuminate\Http\Request))
#2 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(55): call_user_func_array(Array, Array)
#3 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(44): Illuminate\Routing\Controller->callAction('checkGeneral', Array)
#4 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Route.php(189): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\ServerController), 'checkGeneral')

In the above log file named laravel.log, it is said that there is an error which is shown exactly stated in the title of this article.  The error itself is generated by a script or snippet code within an article named ‘ServerController.php’ which is a Controller file in this part of section :

public function checkGeneralInfo(Request $request) {
        $server->type = $request->input("type");
        $server->serial_number = $request->input("serial_number");
        $server->label = $request->input("label");
        $server->ip_internal = $request->input("ip_internal");
        $server->ip_public = $request->input("ip_public");
        $server->ip_management = $request->input("ip_management");
        $server->label = $request->input("label");
        $server->year = $request->input("year");
        $server->warranty = $request->input("warranty");
}

The above snippet code is a method within controller file named checkGeneralInfo. It is written to retrieved information from a blade view template file which has a form defined in it. The form itself has several input fields with the names of each field ranging from the type until the warranty of the server. User will input all of the data in the input fields inside the form defined in the blade view file template. And apparently, the error specified, ‘Creating default object from an empty value’, it is because the object hasn’t been instantiated or hasn’t been created. So, it is considered as an empty value. The object in this context is represented with a variable named $server. To solve the error generated, the following line of code is needed to be inserted to be able to create or to instantiate the object represented with a variable named $server :

        $server = new Server();

 So, the overall snippet code will be written as follows :

public function checkGeneralInfo(Request $request) {
        $server = new Server(); 
        $server->type = $request->input("type");
        $server->serial_number = $request->input("serial_number");
        $server->label = $request->input("label");
        $server->ip_internal = $request->input("ip_internal");
        $server->ip_public = $request->input("ip_public");
        $server->ip_management = $request->input("ip_management");
        $server->label = $request->input("label");
        $server->year = $request->input("year");
        $server->warranty = $request->input("warranty");
}

One thought on “Laravel Error Message : Creating default object from empty value

Leave a Reply