216 views
asked in MongoDB by
Create Laravel 11 Project and configure MongoDB Database

1 Answer

answered by

inchirags@gmail.com  Chirag's Laravel Tutorial https://www.chirags.in

*********************************************************************************************

Create Laravel Project and configure MongoDB Database step by step process

*********************************************************************************************

Prerequisite:

MongoDB Database:

1. Install & Access MongoDB in Windows - https://youtu.be/ht1Ywfj5ac4

2. Install & Access MongoDB in Ubuntu - https://youtu.be/JRY5b-DpGkM

3. Install & Access MongoDB in CentOS - https://youtu.be/6fIzukUtzCU

Here’s a step-by-step guide to creating a Laravel project and configuring it to use MongoDB as the database:

Step 1: Install Laravel

1.1 Install Composer

If you haven't installed Composer (a dependency manager for PHP), you can do so by following Composer Installation Instructions.

1.2 Create a Laravel Project

Once Composer is installed, run the following command in your terminal to create a new Laravel project:

composer create-project --prefer-dist laravel/laravel laravel-mongodb

This will create a new Laravel project named laravel-mongodb.

Step 2: Install MongoDB

2.1 Install MongoDB Server

If MongoDB is not already installed, you can download and install it from the official MongoDB website.

2.2 Install MongoDB PHP Extension

Download DLL for MongoDB PHP Driver

Go to MongoDB PHP Driver Downloads (https://pecl.php.net/package/mongodb).

https://pecl.php.net/package/mongodb

download (8.2 Thread Safe (TS) x64)

Download the correct DLL version for your PHP version and system architecture (e.g., 7.4, 8.0, 8.1, or 8.2, and x86 or x64).

Place the DLL File in PHP Extension Directory

Once you’ve downloaded the .dll file:

Copy the downloaded .dll file into the ext directory of your PHP installation. For example, if you're using XAMPP, this could be something like:

C:\xampp\php\ext\

Enable the Extension in php.ini

Now, open your php.ini file (located in C:\xampp\php\php.ini), and add the following line:

extension=php_mongodb

Save the file and restart your Apache server through the XAMPP control panel.

3.4 Verify the MongoDB Extension Installation

You can check if the MongoDB extension is enabled by running:

php -m | findstr mongodb

If it returns mongodb, it means the extension is successfully installed.

Step 3: Install the Laravel MongoDB Package

To use MongoDB with Laravel, install the mongodb/laravel-mongodb package, which provides integration between Laravel and MongoDB.

Run the following command to install it:

composer require mongodb/laravel-mongodb

Step 4: Configure Laravel to Use MongoDB

4.1 Update the Database Configuration

In your Laravel project, open the config/database.php file. Add the MongoDB connection configuration in the connections array:

'connections' => [

    // Other connections...

    'mongodb' => [

        'driver'   => 'mongodb',

        'host'     => env('DB_HOST', '127.0.0.1'),

        'port'     => env('DB_PORT', 27017),

        'database' => env('DB_DATABASE'),

        'username' => env('DB_USERNAME'),

        'password' => env('DB_PASSWORD'),

        'options'  => [

            'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'), // authentication database (optional)

        ],

    ],

],

4.2 Select the Database

Open the database:

mongosh

use admin

4.3 Create the User

Run the following command to create a new user. Replace your_username, your_password, and readWrite with your desired username, password, and role:

db.createUser({

    user: "mongouser",

    pwd: "admin@123",

    roles: [

        { role: "readWrite", db: "laraveldb_mongo" },

        { role: "dbAdmin", db: "laraveldb_mongo" }

    ]

})

Step 4: Verify the User Creation

You can check if the user was created successfully by running:

db.getUsers()

4.1 Login with the admin user:

mongosh --port 27017 -u "admin" -p "admin@123" --authenticationDatabase "admin"

4.2 Set Environment Variables

Update your .env file with the MongoDB connection details:

DB_CONNECTION=mongodb

DB_HOST=127.0.0.1

DB_PORT=27017

DB_DATABASE=laraveldb_mongo

DB_USERNAME=mongouser

DB_PASSWORD=admin@123

Step 5: Create and Run Migrations (Optional)

MongoDB is a NoSQL database, so migrations aren’t always necessary. However, if you still want to create migrations (for structure documentation or indexing), you can proceed as usual with Laravel’s migration system:

Then, run the migration:

php artisan migrate

Step 7: Test the Database

Access the database in MongoDB Compass and check the database.

By following these steps, you should be able to set up MongoDB as the database for your Laravel project successfully.

For any doubts and query, please write on YouTube video comments section.

Note : Flow the Process shown in video.

Please, Subscribe and like for more videos:

https://www.youtube.com/@chiragstutorial

Don't forget to, Follow, Like, Share &, Comment

Thanks & Regards,

Chitt Ranjan Mahto "Chirag"

_____________________________________________________________________

Note: All scripts used in this demo will be available in our website.

Link will be available in description.

#chirags 

#chiragstutorial 

#chiragslaraveltutorial

#chiragslaraveltutorials

#laraveltutorial 

#laravel11 

#laravelcourse 

#installlaravel

#laravel_tutorial 

#laravelphp

#chiragdbatutorial

#chiragsdbatutorial

#chriagstutorial

chirags, chirags tutorial, chirags laravel tutorial, chirags Laravel tutorial, Laravel tutorial, laravel11, Laravel course, install laravel, laravel_tutorial, Laravel php, chirags dba tutorial, chirags tutorial, chirag tutorial

Most popular tags

postgresql laravel replication laravel-10 ha postgresql mongodb ubuntu 24.04 lts mongodb database mongodb tutorial streaming-replication laravel-11 mysql database laravel postgresql backup laravel login register logout database mysql php technlogy asp.net asp.net c# mysql master slave replication centos linux laravel sql server schedule backup autobackup postgresql django python haproxy load balancer install self sign ssl laravel 11 - login with otp valid for 10 minutes. laravel 11 gaurds user and admin registration user and admin login multiauth postgresql 16 to postgresql 17 postgresql migration zabbix 7 how to install graylog on ubuntu 24.04 lts | step-by-step asp.net core mvc .net mvc network upload c# ssl integration sql server on ubuntu 22.04 lts mssql server ms sql server sql server user access in postgres mysql password change cent os linux configure replica kubernetes (k8s) install nginx load balancer install install and configure .net 8.0 in ubuntu 24.04 lts php in iis php with iis php tutorial chirags php tutorials chirags php tutorial chirags tutorial laravel 11 guards mongodb sharding metabase business analytics metabase ubuntu 24.04 koha 24.05 letsencrypt mongodb crud rocky linux laravel custom captcha laravel 11 captcha laravel captcha mongo dll php.ini debian 12 nginx apache nextcloud gitea in ubuntu git gitea npm error node js mysql ndb cluster mysql cluster ssl oracle login register logout in python debian windows shell batch file bat file time stamp date time shopping cart in laravel centos rhel swap memeory rhel 5.5
...