skip to Main Content

I noticed an issue after I installed breeze on my Laravel application. When trying to register my user I was receiving the following error:

Call to a member function prepare() on null

Which pointed to the following:

$user = User::create([
    "name" => $request->name,
    "email" => $request->email,
    "password" => Hash::make($request->password),
]);

My User Model was generated using php artisan breeze:install .

I noticed it used the native AuthUser model by Illuminate and tried to rewrite the create method, but it required the Authenticatable. I overwrote the IlluminateFoundationAuthUser and replaced the Model with the Mongodb class.

namespace IlluminateFoundationAuth;

use IlluminateAuthAuthenticatable;
use IlluminateAuthMustVerifyEmail;
use IlluminateAuthPasswordsCanResetPassword;
use IlluminateContractsAuthAccessAuthorizable as AuthorizableContract;
use IlluminateContractsAuthAuthenticatable as AuthenticatableContract;
use IlluminateContractsAuthCanResetPassword as CanResetPasswordContract;
//use IlluminateDatabaseEloquentModel;
use MongoDBLaravelEloquentModel;
use IlluminateFoundationAuthAccessAuthorizable;

2

Answers


  1. The error you’re encountering, "Call to a member function prepare() on null," suggests that there’s an issue with your database connection or configuration. This error typically occurs when trying to execute a database query on a null object.

    Given that you’re using MongoDB as your database backend, there are a few things you should ensure:

    Check MongoDB Connection: Make sure your Laravel application is properly configured to connect to your MongoDB instance. Verify your database connection settings in your .env file or config/database.php if you’re explicitly setting the MongoDB connection there.

    Ensure MongoDB Extension: Ensure that you have the necessary MongoDB PHP extension installed and enabled. You can check this in your php.ini configuration file.

    Review Laravel Configuration: Double-check your Laravel configuration to ensure it’s correctly configured to use MongoDB. This includes checking your config/database.php file to ensure the MongoDB connection is properly defined.

    Verify MongoDB Permissions: Ensure that the user configured in your Laravel application has the necessary permissions to perform database operations on MongoDB.

    Check MongoDB Driver Compatibility: Ensure that your version of the MongoDB PHP driver is compatible with your version of MongoDB.

    Once you’ve verified these aspects and corrected any potential issues, try registering a user again and see if the error persists. If it does, you may need to provide more information about your setup and any additional error messages for further assistance.

    Login or Signup to reply.
  2. install composer require mongodb/laravel-mongodb:^4.2

    I have the same issue but i am not using laravel starter kit breeze I solved it using the above package

    use MongoDBLaravelEloquentModel;

    class User extends Model

    If you want to use Laravel’s native Auth functionality, register this included service provider:

    MongoDBLaravelAuthPasswordResetServiceProvider::class

    https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/user-authentication/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search