I'm going back to an old project, and the homepage need to be access when logged in, if not, you get redirected to /login. For that I'm using the Auth middleware, but when doing so, I get the following error (see bottom of post with the error and the full error on imgur).
I checked my middleware
but don't actually have custom one, I also checked my code to see if I weren't using a weird middleware
group or anything, and I can't seem to find an error for now.
In my HomeController.php
, if I remove the usage of the Auth middleware
, the script continue (but I have another error further along, about Roles & Permissions, which is normal)
public function __construct()
{
$this->middleware('auth');
}
Normally, I would expect to be redirected, but instead, I get this error : Trying to get property of non-object
Full error : Refer this
I shall mention that I have this error on EVERY page that need authentification, and not on the one that doesn't, it's not limited to my HomeController
Thanks !
EDIT : Here is the full controler where I use the middleware (even if I have the issue elsewhere too)
<?php
namespace App\Http\Controllers;
use App\Sinistre;
use App\Appointment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$sinistres = null;
if (Auth::user()->hasRole('admin')) {
$sinistres = Sinistre::all();
} else {
$sinistres = Auth::user()->sinistres;
foreach(Auth::user()->societe AS $societe){
$sinistres = $sinistres->merge($societe->sinistre);
}
}
$sinistres = $sinistres->unique();
$appointments = Appointment::orderBy('date_start', 'desc')->with(['sinistre', 'sinistre.user'])->take(8)->get()->reverse();
return view('pages.home', [
'sinistres' => $sinistres,
'appointments' => $appointments,
]);
}
}