How to access a field data from users table from a controller in Laravel?

  laravel, php

I have a DashboardController where I need to fetch dashboard data based on a field site from users table for the logged in user. Following is my controller.

use AppHttpControllersController;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;

class DashboardContoller extends Controller {
    protected $today;
    protected $site;

    public function __construct(){
        $this->middleware('auth');
        $this->today = date("Y-m-d");
        $this->site = auth()->user()->site; // this line is showing error
    }
    // Rest of my code goes here

}

When I am trying to access the field site, it is giving me "Trying to get property ‘site’ of non-object" error. Where am I doing wrong?

Source: Laravel

Leave a Reply