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

Ryahn's avatar
Level 2

Call to a member function prepare() on null MongoDB

I am having issues trying to create some dummy users using a factory. I keep running into the issue Call to a member function prepare() on null

Its weird that I can still select data to display, I just cant manipulate data.

Specs

  • Laravel 9
  • MongoDB 5.0.6
  • PHP 8.1.3
  • php-mongodb 1.12.1

Tinkerwell Error

[!] Aliasing 'User' to 'App\Models\User' for this Tinker session.
PHP Error:  Call to a member function prepare() on null in /Users/ryahn/Public/Sites/globalx-dashboard/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 495

User Model

<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable as AuthenticatableTrait;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use Laravel\Sanctum\HasApiTokens;
use OwenIt\Auditing\Auditable as Audit;
use OwenIt\Auditing\Contracts\Auditable;

class User extends Eloquent implements Auditable, Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, Audit, AuthenticatableTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $guarded = [];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

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

Database Config

'mongodb'    => [
            'driver'  => 'mongodb',
            // 'host'     => env('MONGO_HOST', '127.0.0.1'),
            // 'port'     => env('MONGO_PORT', 27017),
            // 'database' => env('MONGO_DATABASE', 'homestead'),
            // 'username' => env('MONGO_USERNAME', 'homestead'),
            // 'password' => env('MONGO_PASSWORD', 'secret'),
            'options' => [
                // here you can pass more settings to the Mongo Driver Manager
                // see https://www.php.net/manual/en/mongodb-driver-manager.construct.php under "Uri Options" for a list of complete parameters that you can use

                'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'), // required with Mongo 3+
            ],
            'dsn'     => env('MONGO_DB_BASE_URI', 'mongodb://') . env('MONGO_DSN'),
        ],

User Factory

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
 */
class UserFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        $email = $this->faker->unique()->safeEmail();
        return [
            'name'              => $this->faker->name(),
            'snowflake'         => mt_rand(17, 99999999999999999),
            'email'             => $email,
            'email_verified_at' => now(),
            'password'          => Hash::make(Str::random(20)), // password
            'remember_token' => Str::random(10),
            'descrim'           => mt_rand(4, 9999),
            'username'          => $this->faker->username(),
            'avatar'            => 'https://i.pravatar.cc/150?u=' . $email,
            'banner'            => 'https://placeimg.com/960/540/tech',
            'mfa_enabled'       => (bool) random_int(0, 1),
        ];
    }

    /**
     * Indicate that the model's email address should be unverified.
     *
     * @return static
     */
    public function unverified()
    {
        return $this->state(function (array $attributes) {
            return [
                'email_verified_at' => null,
            ];
        });
    }
}
0 likes
6 replies
Ryahn's avatar
Level 2

@Sinnbeck I am still green when digging into the base code of laravel. From what I can at least tell from other testings. Is that its with base laravel using transaction by default. I dont think I am able to turn these off. The only way I can think of doing it, is to use DB::* to do everything.

Sinnbeck's avatar

@Ryahn so the error happens when you run tests? Can you show the test?

Ryahn's avatar
Level 2

@Sinnbeck I was able to track it down to

use OwenIt\Auditing\Auditable as Audit;
use OwenIt\Auditing\Contracts\Auditable;

Once removed, it works just fine.

yowelikachala's avatar

If that is the case, create app/Overrides/Models/Audit.php and in that file, put the contents of vendor/owen-it/laravel-auditing/src/Models/Audit.php.

Update the use line from use Illuminate\Database\Eloquent\Model; to use Jenssegers\Mongodb\Eloquent\Model;

once done, you will have to update composer to autoload this file instead of the one in the vendor folder. So in the composer.json file add the following to "autoload": { section

"exclude-from-classmap": ["vendor/owen-it/laravel-auditing/src/Models/Audit.php"], "files": ["app/Overrides/Models/Audit.php"],

if it does not load still, do dumpautoload

Please or to participate in this conversation.