laravelroutesjwtdingo-api

Trying to get property 'hostname' of non-object


When I use php artisan api:routes I receive the following error:

Trying to get property 'hostname_id' of non-object

I have the following code where is this error :

protected $user;
protected $hostname_id;

public function __construct() {
    $this->user = JWTAuth::user();
    $this->hostname_id = $this->user->hostname_id;
   }

I read something that JWTAuth::user() return null and because of that i can not het hostname_id. Maybe is another way to receive loggedIn user data from JWT token?


Solution

  • Error happen because when you are run php artisan api:route it is call all controller __constract() function You can use this code

    protected $user;
    protected $hostname_id;
    
    public function __construct() {
        $this->user = JWTAuth::user();
        $this->hostname_id = $this->user->hostname_id ?? null;
    }