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

jameshaishitan's avatar

How to handle Trying to get property 'id' of non-object

I got this error "Trying to get property 'id' of non-object ..." when I reload my page. I know it is due to session expired and the object $user->id is no longer there. My question is how do I handle this?

I have this in my blade

var App = new Vue({
    el: '#app',
    data: {
      urlCenter: '/manage/getuserrolecenterslist/{{ $user->id}}/4', 

In my Handler.php

public function render($request, Exception $exception)
{
    if ($exception instanceof HttpException) {
            if ($request->expectsJson()) {
                return response()->json(['error' => 'Unauthenticated.'], 401);
            }

            if ($request->ajax()){
                return response('Unauthenticated', 401);
            }
            // redirect to login page
            return redirect()->route('login');
    }elseif ($exception instanceof TokenMismatchException) {
        return redirect()->route('login');
    }
    
        
    return parent::render($request, $exception);
     }
0 likes
3 replies
jove's avatar

What does your view controller look like?

jameshaishitan's avatar

This is my controller for "show", the "edit" also the same.

public function show($id)
{
    $episode = Episode::withTrashed()->with('pat)->where('id', $id)->first();
   
    return view("registration.episode.show")->withEpisode($episode);
}
Snapey's avatar

you have a controller providing $user to the view. It should check if the user is logged in

Please or to participate in this conversation.