I have DashboardController
where I need to fetch dashboard data based on a field site
from users
table for the logged in user.
This is my controller:
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
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
}
It is giving me "Trying to get property 'site' of non-object" error when I try to access the field site
. What am I doing wrong?