Laravel 5 – MongoDB Easy Setup


  • It’s an Open source PHP Web Framework.
  • Development of web applications following Model-View-Controller (MVC) architecture.
  • Official Website


  • It’s Classified as NoSQL database program.
  • It supports field, range queries, regular expression searches.
  • Official Website


  • It’s Free and can run on Linux.
  • Faster Performance


  • The size of database is little bit high..because of field name is repeatedly storing for each document.
  • Flexibility is less..because there are no join queries.


Laravel-MongoDB Setup

  • First, you must create Laravel project on your machine. Laravel can install through composer using this command.

       composer create-project –prefer-dist laravel/laravel <project_name>

    It will take up to 5-10 mins depends on your machine configuration and it will install the latest version of Laravel.

  • Can run your application by this command

       php artisan serve

  • Before proceeding to configure MongoDB with Laravel make sure you have MongoDB PHP driver installed.
    If you didn’t install MongoDB PHP driver means please follow the link to install
    Note: The old mongo PHP driver is not supported in versions >= 3.0.
  • Next step we move on to Mongo DB configurations in Laravel.
    MongoDB can install through composer using this command
     composer require jenssegers/mongodb
  • Then add the service provider in your application folder
    Select your application folder–>Config–>app.php
    In your app.php file, add the below line in the provider’s array
  • Next step is adding your MongoDB connection and making default your connection
    In that same folder, there was a database.php file will be available
    Select that file and add the below coding in connection’s array.
               ‘mongodb’ => [
               ‘driver’ => ‘mongodb’,
               ‘host’ => env(‘DB_HOST’, ‘localhost’),
               ‘port’ => env(‘DB_PORT’, 27017),
               ‘database’ => env(‘DB_DATABASE’,'<db_name>’),
               ‘username’ => env(‘DB_USERNAME’),
               ‘password’ => env(‘DB_PASSWORD’),
               ‘options’ => [
               ‘database’ => ‘<db_name>’
         ]  ,
    Then make your default connection as mongodb
      'default' => env('DB_CONNECTION', 'mongodb'),
  • Last and final step..Configuration in Environment file..(.env file)
    That file is available at your laravel application folder
    You have to configure your mongodb setup in .env file..add the below line in your env file

That all about MongoDB with laravel Setup…Now you can use Eloquent method or query builder method to implement CRUD Operations in laravel and mongodb….For further reference..please visit this link
Thank you all.

Leave a Reply

Your email address will not be published.