This tutorial will explain the steps required for configuring Laravel to perform an insert operation against a MongoDB document. Laravel is a PHP web-application framework using a refined syntax that is designed to make the common tasks of authentication, routing and caching simpler. As MongoDB is an excellent data storage system for API-heavy applications, the two programs are compatible and complement each other nicely when used to insert MongoDB document using Laravel.
In order to execute code in PHP that interacts with the MongoDB server you should install the MongoDB driver in your PHP execution environment. This tutorial will explain the steps required for configuring Laravel to perform an insert operation against a MongoDB document. Laravel is a PHP web-application framework using a refined syntax that is designed to make the common tasks of authentication, routing and caching simpler.
Composer must be properly installed and configured to install Laravel and create the Laravel project(s). Composer can be downloaded at: (https://getcomposer.org/Composer-Setup.exe)
[XAMMP] must be installed and can be download at: (https://www.apachefriends.org/xampp-files/7.1.33/xampp-windows-x64-7.1.33-0-VC14-installer.exe). It can be ran as a service.
Have a working text editor; this tutorial will be using [Visual Studio Code] that can be downloaded at: (https://code.visualstudio.com/download).
- The compatible
php_mongodb.dllfor the installed PHP version on the host system must be download and set up beforehand.
- MongoDB must be properly installed and configured. Enter the following command to obtain the MongoDB version used:
db version v4.2.1
- Enter the following command to obtain the PHP version used:
- The Postman application must be installed and working.
Note: A Windows system was used for executing the examples in this tutorial.
How to Configure MongoDB database in Windows
This section will explain how to configure a MongoDB database in a Windows system to insert MongoDB document using Laravel.
If it has not already been done, the PHP MongoDB driver must be downloaded. For best results, the driver version should be compatible with the currently installed PHP version.
To download the PHP MongoDB driver, execute the following steps in order:
First, extract the downloaded dll file into the PHP extensions directory where XAMPP stores the required dll of the application. The path will be
Next, open the
php.inifile using a text editor, which should be an administrator account, and look for the “extensions” section where the needed extensions for the application can be specified. Then append the following text:
Note that the driver must be bootstrapped once the server starts.
With MongoDB now properly configured, the Laravel project can now be created by executing the following steps in sequence:
- First, navigate to the htdocs (C:xampphtdocs-) and execute the following command:
composer create-project --prefer-dist laravel/laravel laravelmongo
This command will install Laravel in the system and will create a new directory inside the xampp’s htdocs named “laravelmongo.”
Verify the Laravel project was successfully created by entering the following URL in the browser:
The output should resemble the following:
Setting Up Laravel Virtual Host
This section will explain how to create a virtual host that will customize the URL from this:
http://localhost/laravelmongo/public to this:
To do this, execute the following the steps in sequence:
- First, open the
hhtpd-vhosts.conffile, with administrator privileges, within the XAMPP application.
- Now navigate to the
C:xamppapacheconfextradirectory and add the following line of texts:
Now, for the settings to actually take effect, the system host file must be modified. Navigate to the following URL:
hostsfile by appending the following line of texts:
- After saving the changes, restart Apache from the XAMPP panel by clicking on the -kbd-stop-/kbd- and -kbd-start-/kbd- button, as shown in the following image:
Now test the application to confirm it is using the
laravelmongo.net URL configured previously.
Convert utc to swedish time. The output should resemble the following:
If successful, the applications will now be using the customized URL!
Configure MongoDB Database in Laravel
With a running Laravel application properly set up, the MongoDB database can now be configured to work with the application.
- First, open and append the
.envfile as follows:
- Next, add MongoDB to the database.php under the config directory using the following PHP script:
NOTE: Remember the mysql in this script’s
'default' => env('DB_CONNECTION', 'mysql'), must be changed to
'default' => env('DB_CONNECTION', 'mongodb')for it to work properly.
Install MongoDB package in Laravel
This process for installing the MongoDB package in Laravel is very straightforward, however, make absolutely certain to execute the following command from the project directory:
Register MongoDB Provider
The MongodbServiceProvider can be registered by appending the following PHP script within the
app.php under the config directory as follows:
Create a Laravel Model
This section will cover creating a model for the application using the following command in the command prompt or terminal:
This command will generate a new file in the application called
Product.php. Open and append that file with the following PHP script:
use JenssegersMongodbEloquent Model as Eloquent;
class Product extends Eloquent
Creating a Laravel View for Inserting MongoDB Document
Create a new template in the application under the directory
resourcesviews and name it
Now append the following HTML tags to create a form with the
The form created by the above code will be defined in the following section.
Create a Laravel Controller and a Route
This section will explain how to create the Laravel controller by executing the following command:
php artisan make:controller ProductController
The following code will use Laravel to insert a MongoDB document:
publicfunction insert(Request $request)
return('Phone has been successfully added');
Now the route of the application can be defined by modifying the web.php under the
routes directory and executing the following PHP script:
Note that the above scripts will load the form as a default homepage and the next code will route the request from the form, here being
product.store, to the controller for performing the insert function.
Xampp Mongodb Download
The application can now be tested and should resemble the following:
The insert operation can now be verified via the Mongo shell as follows:
> use ProductDb
switched to db ProductDb
> show collections
'_id' : ObjectId('5df48f91e74c00009c007344'),
'phonecompany' : 'samong',
'model' : 'fire lake',
'price' : '2000',
'updated_at' : ISODate('2019-12-14T07:30:24Z'),
'created_at' : ISODate('2019-12-14T07:30:24Z')
Xampp Mongodb Windows
This tutorial explained the steps used to insert MongoDB document using Laravel. The article specifically covered how to configure Laravel to work with a MongoDB database and explained how to configure the MongoDB database in Windows, install Laravel and set up the Laravel virtual host. The tutorial also covered how to register the MongoDB provider, create a Laravel model, create a Laravel controller and create a route and how to verify the insert operation was successful via the Mongo shell. Remember that the PHP MongoDB driver must be bootstrapped once the server starts and before trying to insert MongoDB document using Laravel.”
Oh yeah. Today i installed XAMPP into c:xampp to work with a local environment at my Windows 10 Pro Computer. But instead of MySQL i wanted to use MongoDB.
After installing XAMPP i installed MongoDB Server Community Edition. First i tried to custom install the software at c:mongodb but then the database service won’t start. So after messing around and trying to get it fixed i gave up and installed MongoDB with the default path into C:Program FilesMongoDB Works perfect and the service is running without producing errors. I also downloaded MongoDB Compass Community Edition for having a nice visual database editor.
To insert MongoDB into XAMPP i needed to download the MongoDB driver. to get the installed version of XAMPP i added a index.php file to my C:xampphtdocsxampp directory with the followng content:
Xampp Mongodb Client
After i’ve saved the file i opened my browser with http://localhost/xampp/ to get the PHP info displayed.
Line four shows the architecture of the installed PHP version. This can be x86 or x64. This information is needed to load the correct package for MongoDB from the PECL repository.
After this we browse to https://pecl.php.net/package/mongodb/, select the newest version for and click on the DLL-Link to get the driver DLL for Windows.
Xampp Mongodb Mac
Then we select the driver matching our PHP version and architecture. I have PHP 7.2.x and x86 as seen at the phpinfo()
Downloading and unzipping the downloaded file, we’ll find the php_mongodb.dll. Copy this file and add it to your extension path of your XAMPP installation under C:xamppphpext . After that edit your php.ini file at C:xamppphpphp.ini and add the line
around line 909 at the “Dynamic Extensions” section of your php.ini file. Safe the php.ini-file and restart your Apache server through the XAMPP control panel.
After that check http://localhost/xampp/ again and search for “mongodb”. You should see something like:
Now you’re done and MongoDB is available within your XAMPP environment.
Happy databaseing, Mongo! 🙂