Search code examples
laravellaravel-5middleware

can't access some route because middleware


I'm new at Laravel and i'm currently following some tutorial. in those tutorial i create some middleware named Admin and in Admin middleware i make the code like here:

if (Auth::user() &&  Auth::user()->admin == 1) {

            Session::flash('success','U do not have access to this menu');
            return back();

}

and in my route i'm setting like this:

Route::get('/users/{id}/makeadmin', 'UsersController@admin')->name('users.makeadmin')->middleware('admin');

well it success for non admin, but when using admin i got blocked either although in user table i already set the admin into 1. so do i missing something or i'm doing it wrong? i'm currently using laravel 5.5.


Solution

  • ah sorry this is very stupid, i forgot to put "!" and that why i can't access the route although its admin already. so here's just what i do.

    if (!Auth::user() &&  Auth::user()->admin !== 1) {
    
                Session::flash('success','U do not have access to this menu');
                return back();
    
    }