How to fix the error trying to get property of non-object in laravel'e?

Get this error if not authenticated the user is trying to navigate the route
Route::get('home', 'AdminController@admin')->name('home');

On this route I have a mediator
public function handle(Request $request, Closure $next)
{

 if(\Auth::user()->role_id != 1) {
 return redirect(")->with('success', 'I did not call you here');
}

 return $next($request);
 }

The mediator checks the role_id field in the table and if it is not equal to 1, then throws a banana and redirects to the main. As I understand it, the error arises from the fact that not authenticated user role_id no no.

How can I fix this error?
April 7th 20 at 15:16
2 answers
April 7th 20 at 15:18
Solution
Well, add a test. And even easier call auth in front of your mediator.
// Option 1:
if(!\Auth::check() || \Auth::user()->role_id != 1) {
 return redirect(")->with('success', 'I did not call you here');
}

// Option 2:
Route::get('...', '...')->middleware(['auth', 'Your realtor']);
I'm a little differently made

public function handle(Request $request, Closure $next)
{
 if (Auth::check()) {
 if(\Auth::user()->role_id != 1) {
 return redirect(")->with('success', 'I did not call you here');
}
 } else {
 return redirect(");
}
 return $next($request);
 }
- brooke.Eichmann12 commented on April 7th 20 at 15:21
Well, I told you about this and talked - broderick_Marvin70 commented on April 7th 20 at 15:24
April 7th 20 at 15:20
You're checking an already authorized user. So first you need to check authority of the user. If not, send to the authorization page and if Yes, then check the role_id property.

Find more questions by tags Laravel