creating default object from empty value laravel 6/7/8

creating default object from empty value laravel: This is post where the main purpose to write the post itself is to discuss further on how to handle the error stated as in the title of this post.

creating default object from empty value laravel

The error given is an impact from running a script of web-based PHP framework application powered by Laravel.

The below explanation will use a certain file and certain Source code for an example :

Laravel Error Message : Creating default object from empty value as stated as an ErrorException is located as bellows :

Creating default object from empty value

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

ErrorException in /var/www/html/upcommng-tamilrokers/app/Http/Controllers/productController.php line 171 Creating default object from empty value

The Laravel Error Message : Creating default object from empty value is retrieved from Web browser like firebug tool installed as a plugins in Mozilla Firefox.

Read Also:  Laravel 6 Bootstrap Toggle Switch With Ajax Update To Mysql

as well as actually, the error itself can also be displayed or viewed in the below laravel log file named like as a laravel.log normally located in folder main root folder like as a storage/log :

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

Creating default object from empty value

Solution 1

$output = new \stdClass();
$output->success = false;

Creating default object from empty value
This Error message has been E_STRICT for version of the PHP <= 5.3. Since PHP 5.4, this was unluckilly updated to E_WARNING. Since E_WARNING error messages are useful, you don't want to any types of the errors disable them completely.

if (!isset($output)) 
    $output = new stdClass();

$output->success = false;
In the above simple generated log file named is laravel.log, this is said that there is an error which is shown exactly stated in the title of this post.

Read Also:  How to check multidimensional array is empty or not in php?

The error itself is generated by a script or Source code within an post named ‘productController.php’ which is a Controller file in this part of section :

public function statusOfProductImpExport(Request $request) {
        $product->type = $request->input("type");
        $product->product_code = $request->input("product_code");
        $product->sku = $request->input("sku");
        $product->ip_internal = $request->input("ip_internal");
        $product->ip_public = $request->input("ip_public");
        $product->mpn_gtin = $request->input("mpn_gtin");
        $product->sku = $request->input("sku");
        $product->year = $request->input("year");
        $product->price = $request->input("price");
}

The above Source code is a method within controller file named statusOfProductImpExport. This is written to retrieved details from a blade file on view HTML template file which has a form describe in it.

The form itself has different some input fields with the names of each field ranging from the type until the price of the product.

User will input all of the data in the input fields inside the form describe in the blade view file template.

And seemingly, the error identify, ‘Creating default object from an empty value’, this is because the object hasn’t been instantiated or hasn’t been created.

Therefor, this is contemplated as an empty or null value. The object in this context is constitute with a variable named $product.

Read Also:  PHP Ajax dynamic Progress Bar for PHP AJAX File Upload

creating default object from empty value laravel : Solution

To best solve the error generated on server side code, the below line of source code is required to be inserted to be able to make or to instantiate the object constitute with a variable named $product :

       $product = new product();

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

public function statusOfProductImpExport(Request $request) {
        $product = new product(); 
        $product->type = $request->input("type");
        $product->product_code = $request->input("product_code");
        $product->sku = $request->input("sku");
        $product->mpn_gtin = $request->input("mpn_gtin");
        $product->ip_public = $request->input("ip_public");
        $product->ip_management = $request->input("ip_management");
        $product->sku = $request->input("sku");
        $product->year = $request->input("year");
        $product->price = $request->input("price");
}