Hello, I encountered a problem, my code is:
if($user->plan->id == 1) { return view($this->activeTemplate . 'user.autopool', compact('pageTitle', 'commissions', 'deposit', 'transactions', 'commission', 'withdraw', 'transaction', 'username', 'balance', 'user', 'plans')); } else { return view($this->activeTemplate . 'user.nopool', compact('pageTitle', 'user')); }
I want to eliminate this error from my code. I'm stuck here.
P粉1514660812023-12-25 00:10:31
$user->plan
evaluates to null. You don't guard against this.
if ($user->plan?->id)
optional()
function.if (optional($user->plan)->id)
User
and Plan
is a belongsTo relationship (User belongsTo Plan), you might be better off just using the foreign key in the user model.if ($user->plan_id)