Only the session driver has attempt method, so you need to use the correct guard as your default one is the api
auth('web')->attempt($credentials)
this might work :)
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am using Laravel-8 for restful api, and it it I have Laravel Passport Authentication:
public function login(Request $request)
{
$credentials = [
'email' => $request->email,
'password' => $request->password
];
if (auth()->attempt($credentials)) {
$token = auth()->user()->createToken('MyApp')->accessToken;
return response()->json(['token' => $token], 200);
} else {
return response()->json(['error' => 'UnAuthorised'], 401);
}
}
auth.api
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
'hash' => false,
],
],
I have this route in api.php:
Route::group(['prefix' => 'v1/admin', 'as' => 'admin.', 'namespace' => 'Api\v1\Admin\', 'middleware' => ['api']], function () {
Route::post('login', 'AdminLoginController@login');
});
When I tried to test on POSTMAN using POST Request, I got this error:
BadMethodCallException: Method Illuminate\Auth\RequestGuard::attempt does not exist. in file
How do I resolve this?
Thanks
So it seems like the initial error is fixed now since you logged in the user you need to use the same guard again to create the token
$token = auth('web')->user()->createToken('MyApp')->accessToken;
Please or to participate in this conversation.