Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mehmetanbaki's avatar

Roles issue

BadMethodCallException Method Illuminate\Auth\SessionGuard::role does not exist. http://127.0.0.1:8000/login

This appear to me when I try to logged in with role

this is the code :

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller; use App\Providers\RouteServiceProvider; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Support\Facades\Auth; use App\Role;

class LoginController extends Controller { /* |-------------------------------------------------------------------------- | Login Controller |-------------------------------------------------------------------------- | | This controller handles authenticating users for the application and | redirecting them to your home screen. The controller uses a trait | to conveniently provide its functionality to your applications. | */

use AuthenticatesUsers;

/**
 * Where to redirect users after login.
 *
 * @var string
 */
// protected $redirectTo = RouteServiceProvider::HOME;

protected function redirectTo(){

    if (Auth::role()->role_name == 'admin') {
        return route('dashboard');
    } elseif (Auth::role()->role_name == 'accountant') {
        return route('main');
    } elseif (Auth::role()->role_name == 'customer') {
        return route('index');
    } else {
        return redirect('/login')->with('status', 'يرجى إدخال إسم المستخدم وكلمة المرور');
    }

}

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest')->except('logout');
}

}

0 likes
12 replies
fylzero's avatar

@mehmetanbaki Where is role() defined? If you are following the Eloquent example, are you sure you didn't define it as roles()?

24 likes
mehmetanbaki's avatar

I tried it out but the same issue

this what appear

BadMethodCallException Method Illuminate\Auth\SessionGuard::role does not exist. http://127.0.0.1:8000/login Hide solutions Bad Method Call

Did you mean Illuminate\Auth\SessionGuard::recaller() ?

fylzero's avatar

@mehmetanbaki You may also need to use Auth::user()->roles(). That seems like it would make more sense. I'm not exactly sure it Auth just returns a User object or not.

24 likes
mehmetanbaki's avatar

no the roles in other table migration I call it roles which contains the role_name

fylzero's avatar

@mehmetanbaki Your migration simply creates the database table, which is usually plural. The relationship you specify in the User model is what this will reference. So if it is roles() in the User model method, it will be roles when you reference it from the User model class.

24 likes

Please or to participate in this conversation.