Follow the instructions to set up your Laravel MongoDB project with a MongoDB Atlas deployment.
To learn more about this project, see the Quick Start in the MongoDB driver documentation.
git clone https://github.com/mongodb-university/laravel-quickstart.git
Alternatively, you can download the project as a zip archive. On the GitHub page, click the Code button and select Download ZIP to start the download.
You need the following software installed in your development environment:
To see data in the example view, you need to have a MongoDB deployment with the "Mflix" Atlas sample datasets loaded. To learn how to load this dataset, see Load Sample Data.
If you need to run a local MongoDB deployment, you can download MongoDB Community Server or MongoDB Enterprise Server. To learn how to load sample data in a local deployment, see Migrate Data with Self-Managed Tools.
Run the following command to navigate to the application directory root directory in the GitHub repository:
cd laravel-quickstart/my-app
Run the following composer
command to install the dependencies:
composer install
Run the following command to generate a Laravel application key:
php artisan key:generate
Run the following command to use the example environment file in your sample app:
cp .env.example .env
In your .env
file, set or update the following variables, replacing the
<connection string>
placeholder with your MongoDB connection string:
DB_CONNECTION=mongodb
DB_URI=<connection string>
If you need to create a MongoDB Atlas deployment, see the Create a MongoDB Deployment step of the Quick Start.
If you already have a MongoDB Atlas deployment, see the Create a Connection String step of the Quick Start.
To start the webserver, run the following command from the application root directory:
php artisan serve
Open http://127.0.0.1:8000/browse_movies in your web browser.
You should see a list of movies and details about each of them.
Run the following command from the application root directory. This command
posts data from the movie.json
file to the API endpoint.
curl -H "Content-Type: application/json" --data @movie.json http://localhost:8000/api/movies
You can view this data by refreshing the http://127.0.0.1:8000/browse_movie page.
If you have trouble getting connected to your MongoDB Atlas deployment, check the following settings:
-
The placeholder for the connection string contains credentials for a user that has appropriate access to the MongoDB database.
-
Make sure your IP address is in the MongoDB IP Access List. To learn more about configuring the access list, see Configure IP Access List Entries in the MongoDB Atlas documentation.