Monday, September 9, 2024

Laravel with Mongodb

 Laravel version - 11.22.0

Php version - 8.3

Setup

  1. composer create-project laravel/laravel laraproject
  2. sudo pecl install mongodb
  3. Find php.in and modify it.
    1. phpinfo() or php -i will give location of php.ini
    2. Append the file to add at the bottom - extension="mongodb.so"
  4. composer require mongodb/laravel-mongodb
  5. modify the file 'config\database.php' to add the following

    'mongodb' => [
                'driver' => 'mongodb',
                'dsn' => env('DB_URI', 'mongodb://localhost:27017/db_name'),
                'database' => env('DB_DATABASE', 'db_name'),
            ],
  6. Set the default database connection name in config\database.php