Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sanch012's avatar

Trying to get property 'id' of non-object

Hi,

I am receiving the following error "Trying to get property 'id' of non-object"

I think this is happening because there are no teams created yet and I am trying to call this in my nav.

How do I solves this?

My Model:

public static Function rotationbymob()

{

return static::where('team_id','=', Auth::user()->currentTeam->id)
            ->groupBy('mob')
            ->get();

}

AppServiceProvider

public function boot()
{
    view()->composer('spark::nav.user-left', function($view) {

        $view->with('rotationbymob', \App\Rotation::rotationbymob());
    });

    
}

blade;

                        <h6 class="dropdown-header">Mobs</h6>

@foreach ($rotationbymob as $rotation) {{ $rotation-> mob}}

@endforeach

                    </div>
0 likes
11 replies
Sirik's avatar

I think Auth::user()->currentTeam returns null

sanch012's avatar

How do I ignore that line if the object is null but include it if their is a current team?

adriannatabio's avatar

The problem is because the current logged in user doesn't have any teams yet so you'll have to check it first before handing it in the query like this.

return static::when(Auth::user()->currentTeam()->exists(), function ($q) {
            $q->where('team_id','=', Auth::user()->currentTeam->id)
        })
        ->groupBy('mob')
        ->get();
sanch012's avatar

Hi @adriannatabio thanks for your answer. I am getting Parse error: syntax error, unexpected '}' when using that code. any ideas why?

tykus's avatar

There is a missing semi-colon after $q->where('team_id','=', Auth::user()->currentTeam->id)

sanch012's avatar

thanks @tykus. now I am receiving "Call to a member function exists() on null (View: /Users/charlie/Code/farming/resources/views/vendor/spark/nav/user.blade.php)"

do i need to add Auth::check somewhere?

tykus's avatar

You don't need Auth::user()->currentTeam()->exists(), only optional(auth()->user())->currentTeam

sanch012's avatar

Thanks @tykus and @adriannatabio

my final code was :


return static::when(optional(auth()->user())->currentTeam, function ($q) {
            $q->where('team_id','=', Auth::user()->currentTeam->id);
        })
        ->groupBy('mob')
        ->get();

arukomp's avatar
arukomp
Best Answer
Level 10

@sanch012

The code above would return all the teams if the user doesn't have a current team. Is that what you intended to do? Doesn't look like it from your original code.

All you really need is just an optional wrapper around the currentTeamin your original code:

return static::where('team_id','=', optional(Auth::user()->currentTeam)->id)
            ->groupBy('mob')
            ->get();
1 like
sanch012's avatar

@arukomp yes you are correct. Your code works, I will use that. Thanks!

I think the other code was working to as all of my tables have a team_id and nothing would display if the current_team_id didn't match.

ps. how do you display code block the way you have?

arukomp's avatar

three backticks to start with, code in a new line, and then again three backticks

1 like

Please or to participate in this conversation.