F
F
Forge01002015-08-21 12:18:12
Laravel
Forge0100, 2015-08-21 12:18:12

Why does "Trying to get property of non-object" error appear in Laravel(s)?

$attendedfestivals = Planner::where('user_id', Auth::user()->id)->where('planned_festivals', 0)->get();

Why does this line throw an error?
I tried to put "-> first()" instead of "-> get()" !!!
97ad9b826e73407cbd407cbaf1146f4f.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Ukolov, 2015-08-21
@Forge0100

if (auth()->check())
{
    $attendedfestivals = Planner::where('user_id', auth()->user()->id)->where('planned_festivals', 0)->get();
}
else
{
    // Обрабатываем ситуацию, когда пользователь неавторизован.
}

M
Melkij, 2015-08-21
@melkij

Since this is exactly Trying to get property, and not Call to a member function, it means that only Auth::user()->id is suitable for the role of the problem area. Namely, Auth::user() returns something that is not an object.
And yes, if the user is not authorized, then Auth::user() will return null.

L
LittleFatNinja, 2015-08-21
@LittleFatNinja

trying to take a property not from an object
, check if you have it setAuth::user()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question