MongoDB CRUD in Laravel

In this section, we will learn how we can perform the CRUD operation. We will use Laravel and MongoDB databases to do this. We usually store data by using MongoDB because it takes less memory. In today's market, Laravel is a very famous technology. In this example, we will perform MongoDB CRUD operation by using the Laravel 5.6 application. We can use various versions of the Laravel applications, such as Laravel 5, 6, 7, and 8. In our Laravel application, we will create books, create, update, remove and view.

To create CRUD (create, update, read, and delete), we will use the composer package "jenssegers/laravel-mongodb". We can use this method to use the model's eloquent method, such as whereNull, whereIn, collection, oderBy, take, distinct, all, first, whereBetween, get, orWhere skip, etc.

In our below example, we will create the database named MongoDB with the "books" collection. After that, we will use the env file to configure the details of the MongoDB database. Then we will use the Laravel 5.6 application to create the CRUD module. The step by step processes to create the CRUD is described as follows:

Step 1:

In this step, we are going to create a MongoDB database. In our example, we need to create our book collection and MongoDB database. When we successfully install the MongoDB database, we will use our command prompt, connect to MongoDB. For connection, we will create a database, and then we will create the collection. After that, we will insert the book by using the following command like this:

Step 2:

In this step, we are going to Install the Laravel 5.6 project. For this, we will use the fresh version of the Laravel 5.6 application. The following command will be useful to do this. To run the following command, we will use our terminal, which is described as follows:

Step 3:

In this step, we are going to perform MongoDB Database Configuration. In our Laravel 5.6 application, we will set the name of the database, username, and password to perform the CRUD operation. For this, we will open the .env file, and then we will add all details, which is described as follows:

.env

After that, we will use the database.php config file and add details of the array, which is described as follows:

config/database.php

Step 4:

In this step, we are going to Install laravel-mongodb Package. In our below example, we will use Composer package manage for its installation. For this, we will use our command prompt and run the following command like this:

When we successfully install the above package, we will use the app.php configuration file so that we can add the service provider, which is described as follows:

config/app.php

Step 5:

In this step, we are going to Create a Books Model. In our application, we will provide a connection to Laravel Eloquent by using the Book model. The code to create a book model is described as follows:

app/Book.php

Step 6:

In this step, we are going to Add Resource Route. We will do this for the book CRUD application. We will add our route by using the file named "routes/web.php" like this:

routes/web.php

Step 7:

In this step, we are going to Create BookController. We will Create BookController as a new controller. Using this controller, we can create the resource controller. The code to create it is described as follows:

When we successfully execute this command, we will get a new file in "app/Http/Controllers/BookController.php" path. For this, we will create by default 7 methods in this controller, which is as follows:

  • index()
  • edit()
  • store()
  • destroy()
  • create()
  • update()
  • show()

For this, we will use a file named BookController.php, and then we will put the following code into it:

app/Http/Controllers/BookController.php

Step 8:

In this step, we are going to Create Blade Files. For this, we will firstly create the file of layout. After that, we will create a new "books" folder. Then we will create blade files for our CRUD operation. The blade files which we finally create are described as follows:

  • blade.php
  • blade.php
  • blade.php
  • blade.php
  • blade.php
  • blade.php

For this, we will add the following code by creating the below file.

resources/views/books/layout.blade.php

resources/views/books/index.blade.php

resources/views/books/show.blade.php

resources/views/books/create.blade.php

resources/views/books/edit.blade.php

Now our above code is ready to run. In order to run the above code quickly, we will use the following command:

Now we can use our browser to open the bellow URL:

After open this, we can see the following output:

MongoDB CRUD in Laravel




Latest Courses