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

andy0517's avatar

Laravel passport api undefined method attempt

Im using passport for creating the API authentication. I facing the problem of showing the error in the login function. It shows the Error

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to undefined method Illuminate\Auth\TokenGuard::attempt()

Here is my controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Member;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class APIController extends Controller
{
    //
    public function register(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required',
            'c_password' => 'required|same:password',
            'contact_no' => 'required'
          ]);
        if ($validator->fails()) {
          return response()->json(['error'=>$validator->errors()], 401);
        }

        $member = Member::create([
          'name' => $request->name,
          'email' => $request->email,
          'password' => bcrypt($request->password),
          'contact_no' => $request->contact_no
        ]);
        $success['token'] =  $member->createToken('asApp')->accessToken;
        $success['name'] =  $member->name;
        return response()->json(['success'=>$success], 200);

        //return response()->json(['success'=>'work'], 200);
    }

    public function login(Request $request)
    {

      if(Auth::guard('member-api')->attempt(['email' => $request->email, 'password' => $request->password]))
      {
        $member = Auth::member();
           $success['token'] =  $member->createToken('MyApp')->accessToken;
           return response()->json(['success' => $success], 200);
      }else {
        return response()->json(['error'=>'Unauthorised'], 401);

      }
    }
    public function details()
    {
      return response()->json(['user' => auth()->member()], 200);
    }

}

The error is the attempt function in the login. Anyone know what wrong with it? or ways to solve it

0 likes
0 replies

Please or to participate in this conversation.