If you’re building web applications with the Laravel PHP web framework, you may want your app to interact with a MongoDB database. Fortunately, it’s easy to use Laravel to perform various database operations. In this article, we’ll explain how to delete a MongoDB document using Laravel. Before we move forward with our tutorial, let’s take a moment to review some of the key prerequisites
needed for this project: NOTE: In this article, we’ll be working in a Windows environment. Once all the required items have been installed, we can move forward with our project. We’ll start by configuring the MongoDB database to make it accessible to us when we start coding our Laravel application. In the previous
section, we mentioned that MongoDB must be downloaded and installed. Now, we’ll extract the DLL file in the following directory: C:\xampp\php\ext We’ll need to register this DLL file within PHP as an extension. To do this, we’ll modify our php.ini file and append this text in the ‘extensions’ section: exetnsion=php_mongodb.dll. In this section, we’ll show you how to create a locally-hosted Laravel project. Laravel projects can be
created in the command prompt with the following Composer command: NOTE: Be sure you’re in the xampp\htdocs directory when we executing this command.
1 composer create-project --prefer-dist laravel/laravel deletemongoIntroduction
Prerequisites
How to Configure MongoDB Database in PHP
Create Laravel Project
The code shown above creates the deletemongo directory inside htdocs.
We can test our Laravel project in the browser using the following URL: //localhost/deletemongo/public.
The output should look like the following:
Configure MongoDB Database in Laravel
Next, we’ll configure Laravel to connect with our MongoDB database. We’ll need to modify two files for our database connection:
- the .env file
1 | DB_CONNECTION=mongodb |
- the database.php file
1 | 'mongodb' => [ |
NOTE: Remember to change the value of DB_CONNECTION from this:
'default' => env('DB_CONNECTION', 'mysql'),
to this:
'default' => env('DB_CONNECTION', 'mongodb').
Install MongoDB Package in Laravel
We can now install the MongoDB package using the following Composer command:
1 | composer require jenssegers/mongodb |
Register MongoDB Provider
To be able to register the MongodbServiceProvider within Laravel, we’ll need to add the following PHP script to our app.php file inside the config directory:
1 | 'providers' => [ |
Create a Laravel Model
Our next step is to create a model for our application that will allow us to interact with our MongoDB database. To do this, we need to execute the following command within our project directory:
1 | php artisan make:model Employee |
The command shown above command creates a file called Employee.php. Within this file we can declare the MongoDB document field with which to perform our CRUD operations:
1 | <?php namespace App; use Illuminate\Database\Eloquent\Model; class Employee extends Eloquent protected $fillable = [ |
Creating a MongoDB Database and Collection
In this section, we’ll be creating a sample MongoDB database and collection that we can use in our code examples:
1 | > use EmployeeDb |
After inserting the documents, we can verify that they’re in the collection:
1 | > db.employee.find().pretty() |
In this section, we’ll show how to delete a MongoDB document by configuring the Laravel controller and the route.
Create and Configure Laravel Controller
Let’s configure the controller to be able to perform the delete operation:
- First, we’ll create our controller by executing the following command:
1 | php artisan make:controller EmployeeController |
This command will create a PHP file called Employee.php. Then we’ll append the following PHP script to the file:
1 |
public function destroy($id) return
('employee record has been successfully delete'); |
Now we’ll need to configure the route where the user will send the delete request. To handle this, we’ll modify the file api.app in the routes directory with the following line of code:
1 | Route::delete('/deleteEmployee/{id}', 'EmployeeController@destroy'); |
We can now test out our application using Postman. Our test will involve deleting a MongoDB document with an id of 5df5da7eedf57fa3283a830f:
We can also verify that our delete operation was successful using the Mongo shell:
1 | > db.employee.find().pretty() |
Conclusion
When you’re building apps with Laravel, it’s helpful to know how to perform various MongoDB operations such as deleting a MongoDB document. In this article, we provided step-by-step instructions for this process, showing you how to set up Laravel and perform a delete operation on a MongoDB document. With our examples to use as a guide, you’ll be able to incorporate MongoDB operations into your own Laravel development.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started