Hướng dẫn delete column mongodb laravel
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. Nội dung chính Nội dung chính
PrerequisitesBefore 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. How to Configure MongoDB Database in PHPOnce 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: 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: Create Laravel ProjectIn 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
The code shown above creates the deletemongo directory inside htdocs. We can test our Laravel project in the browser using the following URL: The output should look like the following: Configure MongoDB Database in LaravelNext, we’ll configure Laravel to connect with our MongoDB database. We’ll need to modify two files for our database connection:
NOTE: Remember to change the value of
to this:
Install MongoDB Package in LaravelWe can now install the MongoDB package using the following Composer command:
Register MongoDB ProviderTo be able to register the MongodbServiceProvider within Laravel, we’ll need to add the following PHP script to our
Create a Laravel ModelOur 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:
The command shown above command creates a file called
Creating a MongoDB Database and CollectionIn this section, we’ll be creating a sample MongoDB database and collection that we can use in our code examples:
After inserting the documents, we can verify that they’re in the collection:
In this section, we’ll show how to delete a MongoDB document by configuring the Laravel controller and the route. Create and Configure Laravel ControllerLet’s configure the controller to be able to perform the delete operation:
This command will create a PHP file called
Now we’ll need to configure the route where the user will send the delete request. To handle this, we’ll modify the file We can now test out our application using Postman. Our test will involve deleting a MongoDB document with an id of We can also verify that our delete operation was successful using the Mongo shell:
ConclusionWhen 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 |