Creating default object from empty value in PHP?
Your new environment may have E_STRICT
warnings enabled in error_reporting
for PHP versions <= 5.3.x, or simply have error_reporting
set to at least E_WARNING
with PHP versions >= 5.4. That error is triggered when $res
is NULL
or not yet initialized:
$res = NULL;
$res->success = false; // Warning: Creating default object from empty value
PHP will report a different error message if $res
is already initialized to some value but is not an object:
$res = 33;
$res->success = false; // Warning: Attempt to assign property of non-object
In order to comply with E_STRICT
standards prior to PHP 5.4, or the normal E_WARNING
error level in PHP >= 5.4, assuming you are trying to create a generic object and assign the property success
, you need to declare $res
as an object of stdClass
in the global namespace:
$res = new \stdClass();
$res->success = false;
PHP Warning: Creating default object from empty value in
The error is not about $status
object, but $status->about
. Assign an instance of stdClass to it first and the error will go.
$status = new \stdClass();
$status->about = new \stdClass();
$status->about->requester = 'username';
$status->about->method = 'method';
$status->about->command = 'cmd';
Error message: Creating default object from empty value
You're getting this error Warning: Creating default object from empty value
because $review->title
returns false
.
find()
takes an id and returns a single model. If no matching model exist, it returns null
.
You can use findOrFail()
instead of find()
, if the record not matched then findOrFail()
will throw a 404 error :
$review = Review::findOrFail($request->id);
I think you've missed to define a parameter on your function, that passes with your route, probaby its {id}
:
public function updateReview(Request $request, $id)
{
$review = Review::findOrFail($id);
//...
}
Laravel 5.8: Creating default object from empty value
When you search for existing Withdraw Wallet with an id that doesnt exist, it will return null. and since you assign 'cancelled'
as an object attribute on the variable, php will convert $findRequest
to a default object (StdObject::class) to be able to assign the attribute to it.
One simple solution would be to use findOrFail()
instead of find()
wich will trigger an exception when the id doesnt exist in your DataBase and return a 404 response on the request.
$findRequest = WithdrawWallet::findOrFail($id);
$findRequest->status = 'cancelled';
$findRequest->save();
return redirect()->back();
another suggestion (if you have soft delete on on the model and the id exists in the database) is to use withTrashed()
$findRequest = WithdrawWallet::withTrashed()->findOrFail($id);
$findRequest->status = 'cancelled';
$findRequest->save();
return redirect()->back();
PHP Array Warning: Creating default object from empty value
You get the warning
Warning: Creating default object from empty value
because you do
$searcharticles->$postid->post_id = $postid;
Your $searcharticles
is a StdClass. You assign the property $postId
and then immediately chain off another property post_id
, which forces PHP to create a new default object.
To avoid it, put this before the chained method calls:
$searcharticles->$postid = new StdClass;
$searcharticles->$postid->post_id =$postid;
$searcharticles->$postid->title =$title;
$searcharticles->$postid->titlescore = $titlescore;
$searcharticles->$postid->quality =$quality;
Related Topics
How to Repair a Serialized String Which Has Been Corrupted by an Incorrect Byte Count Length
Unicode Character in PHP String
How to Get the Cookies from a PHP Curl into a Variable
Return Single Column from a Multi-Dimensional Array
Create a CSV File For a User in PHP
Automatically Deleting Related Rows in Laravel (Eloquent Orm)
Convert PHP Array to JavaScript
Sending Email With PHP from an Smtp Server
Single Result from Database Using MySQLi
How to Bind an Array of Strings With MySQLi Prepared Statement
How to Check If an Email Address Exists Without Sending an Email
How to Get File_Get_Contents() to Work With Https
How to Get the Query Builder to Output Its Raw SQL Query as a String