SymfonyComponentDebugExceptionFatalThrowableError: Call to a member function prepare() on null

  jenssegers-mongodb, jwt, laravel, laravel-6, php

I’m trying to setup a jwt authentication on laravel 6 using mongodb and jwt-auth

this is the user class

<?php

namespace App;

use TymonJWTAuthContractsJWTSubject;
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
}

the controller

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

class AuthController extends Controller
{
    public function __construct()
    {
        $this->middleware('jwt', ['except' => ['login']]);
    }

    /**
     * Get a JWT via given credentials.
     *
     * @return IlluminateHttpJsonResponse
     */
    public function login()
    {
        $credentials = request(['username', 'password']);
        
        if (! $token = auth()->attempt($credentials)) {//here I get an error undefined method attemp
            return response()->json($credentials, 401);
        }

        return $this->respondWithToken($token);
    }

    protected function respondWithToken($token)
    {
        return response()->json([
            'meta' =>[
                "success" => true,
                "errors" =>[]
            ],
            "data" => [
                "token" => $token,
                "minutes_to_expire" => 1440
            ]
        ]);
    }
}

the guards

    'defaults' => [
        //'guard' => 'web',
        'guard' => 'api',
        'passwords' => 'users',
    ],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        //'driver' => 'token',
        'driver' => 'jwt',
        'provider' => 'users',
        //'hash' => false,
    ],
],

and I deleted the api prefix

protected function mapApiRoutes()
    {
        Route::middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }

and I get this error when I call the /auth route

SymfonyComponentDebugExceptionFatalThrowableError: Call to a member function prepare() on null in file /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 331

#0 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Database/Connection.php(662): IlluminateDatabaseConnection->IlluminateDatabase{closure}('select * from "...', Array)
#1 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Database/Connection.php(629): IlluminateDatabaseConnection->runQueryCallback('select * from "...', Array, Object(Closure))
#2 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Database/Connection.php(338): IlluminateDatabaseConnection->run('select * from "...', Array, Object(Closure))
#3 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2159): IlluminateDatabaseConnection->select('select * from "...', Array, true)
#4 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2147): IlluminateDatabaseQueryBuilder->runSelect()
#5 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2619): IlluminateDatabaseQueryBuilder->IlluminateDatabaseQuery{closure}()
#6 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2148): IlluminateDatabaseQueryBuilder->onceWithColumns(Array, Object(Closure))
#7 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(546): IlluminateDatabaseQueryBuilder->get(Array)
#8 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(530): IlluminateDatabaseEloquentBuilder->getModels(Array)
#9 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Database/Concerns/BuildsQueries.php(143): IlluminateDatabaseEloquentBuilder->get(Array)
#10 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php(131): IlluminateDatabaseEloquentBuilder->first()
#11 /home/myuser/myproject/vendor/tymon/jwt-auth/src/JWTGuard.php(124): IlluminateAuthEloquentUserProvider->retrieveByCredentials(Array)
#12 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php(307): TymonJWTAuthJWTGuard->attempt(Array)
#13 /home/myuser/myproject/app/Http/Controllers/AuthController.php(23): IlluminateAuthAuthManager->__call('attempt', Array)
#14 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): AppHttpControllersAuthController->login()
#15 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): IlluminateRoutingController->callAction('login', Array)
#16 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Routing/Route.php(219): IlluminateRoutingControllerDispatcher->dispatch(Object(IlluminateRoutingRoute), Object(AppHttpControllersAuthController), 'login')
#17 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Routing/Route.php(176): IlluminateRoutingRoute->runController()
#18 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Routing/Router.php(681): IlluminateRoutingRoute->run()
#19 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): IlluminateRoutingRouter->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#20 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#21 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): IlluminateRoutingMiddlewareSubstituteBindings->handle(Object(IlluminateHttpRequest), Object(Closure))
#22 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(59): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#23 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): IlluminateRoutingMiddlewareThrottleRequests->handle(Object(IlluminateHttpRequest), Object(Closure), 60, '1')
#24 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#25 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Routing/Router.php(683): IlluminatePipelinePipeline->then(Object(Closure))
#26 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Routing/Router.php(658): IlluminateRoutingRouter->runRouteWithinStack(Object(IlluminateRoutingRoute), Object(IlluminateHttpRequest))
#27 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Routing/Router.php(624): IlluminateRoutingRouter->runRoute(Object(IlluminateHttpRequest), Object(IlluminateRoutingRoute))
#28 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Routing/Router.php(613): IlluminateRoutingRouter->dispatchToRoute(Object(IlluminateHttpRequest))
#29 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(170): IlluminateRoutingRouter->dispatch(Object(IlluminateHttpRequest))
#30 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): IlluminateFoundationHttpKernel->IlluminateFoundationHttp{closure}(Object(IlluminateHttpRequest))
#31 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#32 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): IlluminateFoundationHttpMiddlewareTransformsRequest->handle(Object(IlluminateHttpRequest), Object(Closure))
#33 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#34 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): IlluminateFoundationHttpMiddlewareTransformsRequest->handle(Object(IlluminateHttpRequest), Object(Closure))
#35 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#36 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): IlluminateFoundationHttpMiddlewareValidatePostSize->handle(Object(IlluminateHttpRequest), Object(Closure))
#37 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(63): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#38 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode->handle(Object(IlluminateHttpRequest), Object(Closure))
#39 /home/myuser/myproject/vendor/fideloper/proxy/src/TrustProxies.php(57): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#40 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): FideloperProxyTrustProxies->handle(Object(IlluminateHttpRequest), Object(Closure))
#41 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#42 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(145): IlluminatePipelinePipeline->then(Object(Closure))
#43 /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): IlluminateFoundationHttpKernel->sendRequestThroughRouter(Object(IlluminateHttpRequest))
#44 /home/myuser/myproject/public/index.php(55): IlluminateFoundationHttpKernel->handle(Object(IlluminateHttpRequest))
#45 /home/myuser/myproject/server.php(21): require_once('/home/myuser/gr...')
#46 {main}

In this line,I get an error undefined method attemp

if (! $token = auth()->attempt($credentials))

and I dont know how to set the User class to use it as a mongo model using jenssegers/laravel-mongodb

Source: Ask PHP

LEAVE A COMMENT