I have spent the last hour googling and reading stack posts on this problem, but have been unable to resolve this issue. I attached a try catch and an if statement and I still am getting the call to member function on null error
. Here is my code
try{
$user = Auth::user();
if(null !== ($user->wasStripePlan())){
$data['allSubscriptions'] = (Auth::user()->wasStripePlan()) ? Auth::user()->subscriptions : [];
}
}catch (\PDOException $e){
$data['allSubscriptions'] = false;
}
Any ideas on solutions?
I think you over thought this one. No need to do a try/catch when simple if/else statements can handle it.
if(isset(Auth::user()){
$data['allSubscriptions'] = $data['allSubscriptions'] = (Auth::user()->wasStripePlan()) ? Auth::user()->subscriptions : [];
}else{
$data['allSbuscriptions'] = false;
}
Since you check if the user is logged in with isset, no error will be thrown and no need to try to catch one. Also unless you know what kind of error will be thrown, I'd just use the base Exception class on your catch block. Since it won't resolve the catch unless the Exception type matches. Since all of the types inherit from Exception, you'll catch the first one that comes up.