Laravel cannot understand why there is an attempt to get a non-object property?

I have MembersControllerone containing the following.

public function indexPaid()
{
    $this->middleware('auth');

    if(!Auth::user()->picked_tools)
    {
        if(Auth::user()->subscribedToPlan('one-tool',   'member')) return redirect('/pick-tools')->with('status', 'You have subscribed to use one of my products! Please select which product below you would like to use.');
        if(Auth::user()->subscribedToPlan('all-tools',  'member')) return redirect('/pick-tools')->with('status', 'You have subscribed to use all of my products! Please select which products below you would like to use.');
    }
    else
    {
        return view('membersHome')->with('productsWeCanUse', \App\ToolSelection::GetUsersProducts());
    }
}

Everything works fine locally, as well as in production during testing. However, in my laravel logs, I see this error.

Trying to get property of non-object {"exception":"[object] (ErrorException(code: 0): Trying to get property of non-object at MembersController.php:93)

Line 93

if(!Auth::user()->picked_tools)

How do I use auth middleware, I can’t understand how picked_toolssometimes it cannot be in an Auth object or an Auth object is not present at this point?

picked_tools- column of the table of my users. Its always there, there is no other interaction with it in the script.

What could be wrong here? Again, being tested locally and during production, I cannot reproduce this error, but I often see it in production logs.

+4
source share
2 answers

. .

.

(docs)

public function __construct()
{
    // this will only apply middleware to one action
    $this->middleware('auth')->only('indexPaid');
}

UPD. , , . . .

+2

, . :

if (auth()->check() && !auth()->user()->picked_tools) {
    // Do something
}
+2

Source: https://habr.com/ru/post/1692129/


All Articles