Laravel Database Seeders

Hello Everybody,

I have a project using Laravel v10 and MongoDB. I am able to run all migrations from the command line using artisan command.

But when I am trying to run my database seeders, I am getting into error mentioning,

Call to a member function exec() on null

In my seeder file, I am trying to run a .sql file using prepared statement.

I have already replaced use Illuminate\Database\Eloquent\Model; with use MongoDB\Laravel\Eloquent\Model;.

Also tried to add connection and fillable properties to my model.

But the same process works well with MySQL database.

Kindly guide me in the right direction.

Thankyou

Hello Zahid,
I’ve tried but could not reproduce your error.
Could you provide the backtrace of the error by running php artisan db:seed -vvv

Hello @jerome,

I am sharing the backtrace of the error, as suggested by you.


   INFO  Seeding database.  

  Database\Seeders\CountryTableSeeder ................................ RUNNING  

   Error 

  Call to a member function exec() on null

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:636
    632▕                 return true;
    633▕             }
    634▕ 
    635▕             $this->recordsHaveBeenModified(
  ➜ 636▕                 $change = $this->getPdo()->exec($query) !== false
    637▕             );
    638▕ 
    639▕             return $change;
    640▕         });

  1   vendor/laravel/framework/src/Illuminate/Database/Connection.php:816
      Illuminate\Database\Connection::Illuminate\Database\{closure}()

  2   vendor/laravel/framework/src/Illuminate/Database/Connection.php:783
      Illuminate\Database\Connection::runQueryCallback()

  3   vendor/laravel/framework/src/Illuminate/Database/Connection.php:630
      Illuminate\Database\Connection::run()

  4   database/seeders/CountryTableSeeder.php:15
      Illuminate\Database\Connection::unprepared()

  5   vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
      Database\Seeders\CountryTableSeeder::run()

  6   vendor/laravel/framework/src/Illuminate/Container/Util.php:41
      Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()

  7   vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
      Illuminate\Container\Util::unwrapIfClosure()

  8   vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:35
      Illuminate\Container\BoundMethod::callBoundMethod()

  9   vendor/laravel/framework/src/Illuminate/Container/Container.php:662
      Illuminate\Container\BoundMethod::call()

  10  vendor/laravel/framework/src/Illuminate/Database/Seeder.php:184
      Illuminate\Container\Container::call()

  11  vendor/laravel/framework/src/Illuminate/Database/Seeder.php:193
      Illuminate\Database\Seeder::Illuminate\Database\{closure}()

  12  vendor/laravel/framework/src/Illuminate/Database/Seeder.php:61
      Illuminate\Database\Seeder::__invoke()

  13  database/seeders/DatabaseSeeder.php:16
      Illuminate\Database\Seeder::call()

  14  vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
      Database\Seeders\DatabaseSeeder::run()

  15  vendor/laravel/framework/src/Illuminate/Container/Util.php:41
      Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()

  16  vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
      Illuminate\Container\Util::unwrapIfClosure()

  17  vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:35
      Illuminate\Container\BoundMethod::callBoundMethod()

  18  vendor/laravel/framework/src/Illuminate/Container/Container.php:662
      Illuminate\Container\BoundMethod::call()

  19  vendor/laravel/framework/src/Illuminate/Database/Seeder.php:184
      Illuminate\Container\Container::call()

  20  vendor/laravel/framework/src/Illuminate/Database/Seeder.php:193
      Illuminate\Database\Seeder::Illuminate\Database\{closure}()

  21  vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php:70
      Illuminate\Database\Seeder::__invoke()

  22  vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php:155
      Illuminate\Database\Console\Seeds\SeedCommand::Illuminate\Database\Console\Seeds\{closure}()

  23  vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php:69
      Illuminate\Database\Eloquent\Model::unguarded()

  24  vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
      Illuminate\Database\Console\Seeds\SeedCommand::handle()

  25  vendor/laravel/framework/src/Illuminate/Container/Util.php:41
      Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()

  26  vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
      Illuminate\Container\Util::unwrapIfClosure()

  27  vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:35
      Illuminate\Container\BoundMethod::callBoundMethod()

  28  vendor/laravel/framework/src/Illuminate/Container/Container.php:662
      Illuminate\Container\BoundMethod::call()

  29  vendor/laravel/framework/src/Illuminate/Console/Command.php:211
      Illuminate\Container\Container::call()

  30  vendor/symfony/console/Command/Command.php:326
      Illuminate\Console\Command::execute()

  31  vendor/laravel/framework/src/Illuminate/Console/Command.php:180
      Symfony\Component\Console\Command\Command::run()

  32  vendor/symfony/console/Application.php:1096
      Illuminate\Console\Command::run()

  33  vendor/symfony/console/Application.php:324
      Symfony\Component\Console\Application::doRunCommand()

  34  vendor/symfony/console/Application.php:175
      Symfony\Component\Console\Application::doRun()

  35  vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:201
      Symfony\Component\Console\Application::run()

  36  artisan:35
      Illuminate\Foundation\Console\Kernel::handle()

To throw more light into my problem at hand, I am trying to seed country table via an .sql file. The same is working when run against MySQL database, but not against MongoDB. Also I am running my Laravel project using Sail.

I would have shared that .sql file as well, but as a new user in this community I am not allowed to upload files.

Hope you might find some clues as to where I am going wrong.

Thankyou

The MongoDB driver doesn’t know about SQL at all. You have to convert your input data in a format that is supported by MongoDB.

Hello @jerome ,

Yes, already corrected. I used a .json file as an input to my seeders, wherever necessary.

Gave me a tough time maybe because I came from SQL RDBMS background.

Thankyou.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.