Creating default object from empty value - Laravel 5

- 1 answer

Ad

I dont know how fix this error, my code:

$subscriptions = Auth::user()->subscriptions;

foreach($subscriptions as $subscription)
{
    $subs = Subscription::find($subscription->id);

    $subs->subscriptionstatus_id = 0;

    $subs->save();

}

and laravel response with Creating default object from empty value in line when I assign 0, what can I do?

thanks

Ad

Answer

Ad

This was because the variable $subs is null, and needs to check if has something:

$subscriptions = Auth::user()->subscriptions;

foreach($subscriptions as $subscription)
{
    $subs = Subscription::find($subscription->id);

    // Check if exist the subs record
    if (count($subs) > 0)
    {
        $subs->subscriptionstatus_id = 0;

        $subs->save();
    }
}

I was added that validation and work's fine.

Ad
source: stackoverflow.com
Ad