This repo demonstrates how easy it is to migrate a Laravel application from a relational database (Postgres) to MongoDB.
The main branch of this repository contains a sample Laravel application that uses Postgres as its database, and ElasticSearch for full text search.
The mongodb branch contains the same application, but it has been modified to use MongoDB Atlas as its database and Atlas Search for full text search.
- PHP 8.2 or higher
- Composer
- Postgres
- MongoDB
- Laravel 12.x
- Laravel MongoDB package (jenssegers/mongodb)
- Laravel UI package (laravel/ui)
- Node.js and npm (for frontend scaffolding)
- Git
- A code editor of your choice (e.g., VSCode, PHPStorm)
- Basic knowledge of Laravel, Postgres, and MongoDB
- Docker (for simplified setup)
- (Optional) For manual setup, install PostgreSQL Server locally.
- MongoDB Server can be installed locally, or you can use the MongoDB cloud service MongoDB Atlas.
(TODO: And the tutorial link here when it's ready)
If you have Docker installed, you can get up and running quickly.
We have included a docker-compose.yml file to simplify the setup process. For the main branch, it sets up a Postgres
Database, ElasticSearch container, a Laravel Queue Worker, and runs the laravel scout command to index the posts in elastic search, so you can test the app before we migrate to MongoDB.
The mongodb branch, has its own docker-compose.yml file that sets up the application and a laravel queue worker. We are using MongoDB Atlas for the database in this branch, so no need of a container for MongoDB. You will however need to set up a free cluster on MongoDB Atlas and update the .env file with your connection string.
-
Clone the repository:
git clone [email protected]:mongodb-developer/laravel-postgresql-to-mongodb.git cd laravel-postgresql-to-mongodb
-
Copy the
.env.examplefile to.env:cp .env.example .env
-
Start the Docker containers:
docker compose up -d --build
The above command will build and start the application, Postgres database, and other necessary services in detached mode. The --build flag ensures that Docker images are rebuilt, which is necessary when switching between branches or when dependencies have changed. The
Dockerfile.appwill handle the installation of PHP dependencies, run and build frontend assets, set up the application, and run migrations and seeders. TheDockerfile.queuewill set up a Laravel queue worker to handle any queued jobs. -
Run the following command to import posts and users into ElasticSearch (main branch only):
docker compose exec app php artisan scout:queue-import "App\Models\Post" docker compose exec app php artisan scout:queue-import "App\Models\User"
This command will index all existing posts into ElasticSearch for full text search functionality.
-
Access the application: Open your browser and navigate to
http://localhost:8080. -
You can log in with the following credentials:
- Email:
[email protected] - Password:
password123
- Email:
-
To stop the application and remove containers, networks, and volumes, run:
docker compose down -v
If you prefer to set up without Docker:
- Clone the repository:
git clone [email protected]:mongodb-developer/laravel-postgresql-to-mongodb.git cd laravel-postgresql-to-mongodb
- Install dependencies:
composer install
- Set up environment variables:
cp .env.example .env php artisan key:generate
- Configure database connection in .env:
- For Postgres (main branch):
DB_CONNECTION=pgsql DB_HOST=127.0.0.1 DB_PORT=5432 DB_DATABASE=laravel DB_USERNAME=laravel DB_PASSWORD=secret
- For MongoDB (mongodb branch):
DB_CONNECTION=mongodb DB_HOST= DB_PORT=27017 DB_DATABASE=laravel DB_USERNAME= DB_PASSWORD=
- Run migrations and seed the database:
php artisan migrate --seed
- Batch import posts into ElasticSearch (main branch only):
php artisan scout:import "App\Models\Post" - Install and build frontend assets:
npm install npm run dev
- Serve the application:
php artisan serve
- Access the application:
Open your browser and navigate to
http://localhost:8000.
To switch between database versions:
- Checkout the desired branch:
- For Postgres:
git checkout main
- For MongoDB:
git checkout mongodb
- For Postgres:
- Follow the setup instructions for the chosen branch. Remember to:
- Stop and remove existing containers: docker compose down -v
- Rebuild and start containers: docker compose up -d --build
- If you are following the manual process, run composer install after switching branches as dependencies may differ.
- Update your .env file with appropriate database settings.
- Clear configuration cache with php artisan config:clear
- Run migrations again if needed (postgres main branch): php artisan migrate:fresh --seed
A simple app ( users, blog posts, comments), full text search.
- It allows us to demonstrate CRUD operations and relationships in both Postgres and MongoDB.