How To Install And Use MongoDB In Laravel Lumen

Hello artisan,

In this laravel lumen mongodb tutorial, I will show you how to use mongodb in laravel lumen. From this laravel lumen mongodb tutorial, you will learn lumen with mongodb installation step by step. You know that lumen is a micro framework of laravel for creating api.

Using the MongoDB database with Lumen provides many advantages for data management such as document-oriented data storage, flexible document schemas, and powerful analytics. Lumen also utilizes Composer to manage its dependencies and we can install lumen by using the composer command. So, before using Lumen, make sure you have Composer installed on your machine.

 

Step 1: Install Lumen

To begin, you need to install Lumen into a specified folder, before configuring MongoDB. Open your terminal and run the following code to create a new Lumen project:

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

 

Now start the lumen application server with the following command:

cd lumen-mongodb
php -S localhost:8000 -t public

 

Next, we need to generate an application key using the maxsky/lumen-app-key-generator package. Run the following command to install the package:

composer require maxsky/lumen-app-key-generator

 

Next, add the command to your kernel file in app/Console/Kernel.php.

app/Console/Kernel.php

protected $commands = [
    \Illuminate\Console\KeyGenerateCommand::class,
]

 

Now run the below command:

php artisan key:generate

 

Step 2: Install MondoDB Package

Now we have to install the MongoDB composer package. So install it by running the below command:

composer require jenssegers/mongodb

 

Next, in this step, we have to register the Laravel MongoDB Provider to support the newly installed package by adding the following code to bootstrap/app.php, underneath // $app->withFacades();.

bootstrap/app.php

$app->register(Jenssegers\Mongodb\MongodbServiceProvider::class);

 

Then, uncomment the following line.

// $app->withEloquent();

 

Read also: Building a REST API with Laravel Microservices Lumen

 

Step 3: Configure the MongoDB database

Now create a folder config and inside that folder, create a file database.php and update it like:

config/database.php

return [
  'default' => env('DB_CONNECTION', 'mongodb'),
  
  'connections' => [
    'mongodb' => [
    'driver' => 'mongodb',
    'dsn' => env('DB_URI', 'homestead'),
    'database' => 'lumen_mongodb',
    ],
  ]
];

 

Note: Add the following code to the bottom of your .env and replace the placeholder  with the retrieved MongoDB URI.

 

DB_URI=

 

Step 4: Update the model

Create a new file, named Blog.php, in the app/Models folder and add the following code to it:

app/Models/Blog.php

namespace App\Models;

use Jenssegers\Mongodb\Eloquent\Model;

class Blog extends Model
{
    protected $connection = 'mongodb';
    protected $collection = 'blogs';

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'title', 'body'
    ];
}

 

Read also: Laravel CRUD With MongoDB Tutorial

 

All are set to work with mongodb with lumen. Hope it can help you.

 

#lumen #mongodb #jenssegersmongodb