Laravel Auth registration issues using MongoDB

41 Views Asked by At

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 Auth\User model by Illuminate and tried to rewrite the create method, but it required the Authenticatable. I overwrote the Illuminate\Foundation\Auth\User and replaced the Model with the Mongodb class.

namespace Illuminate\Foundation\Auth;

use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\MustVerifyEmail;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
//use Illuminate\Database\Eloquent\Model;
use MongoDB\Laravel\Eloquent\Model;
use Illuminate\Foundation\Auth\Access\Authorizable;

Here are my configurations

MONGO_DB_CONNECTION=mongodb
MONGO_DB_URI='mongodb+srv://<admin>:<not_so_secret>@cluster0.<someHex>.mongodb.net'
MONGO_DB_PORT=27017
MONGO_DB_DATABASE=<my_database>
MONGO_DB_USERNAME=<username>
MONGO_DB_PASSWORD=<not_so_secret>

I have also installed mongo via composer install MongoDB which is not from MongoDB.

2

There are 2 best solutions below

1
Sandalanka On

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.

0
navdeep singh On

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 MongoDB\Laravel\Eloquent\Model;

class User extends Model

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

MongoDB\Laravel\Auth\PasswordResetServiceProvider::class

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