Hướng dẫn laravel mongodb delete field

Introduction

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

  • Introduction
  • Prerequisites
  • How to Configure MongoDB Database in PHP
  • Create Laravel Project
  • Configure MongoDB Database in Laravel
  • Install MongoDB Package in Laravel
  • Register MongoDB Provider
  • Create a Laravel Model
  • Creating a MongoDB Database and Collection
  • Create and Configure Laravel Controller
  • Pilot the ObjectRocket Platform Free!

Prerequisites

Before we move forward with our tutorial, let’s take a moment to review some of the key prerequisites needed for this project:

  • First, you’ll need to download and install Composer, which will help with installing Laravel and creating Laravel projects.
  • You’ll also need to download and install XAMMP and run it as a service.
  • You can use the text editor of your choice. Throughout this tutorial, we will be using Visual Studio Code as our text editor.
  • Be sure to download the compatible php_mongodb.dll for the installed PHP version on your system before completing this tutorial.
  • Be sure that MongoDB is properly installed and configured before proceeding.
  • In this article, the MongoDB version being used is:
  • The PHP version being used is:
  • Finally, you’ll need to install the application Postman in order to follow along with this tutorial.

NOTE: In this article, we’ll be working in a Windows environment.

How to Configure MongoDB Database in PHP

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.

Create Laravel Project

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 deletemongo

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
2
3
4
5
6

DB_CONNECTION=mongodb
DB_HOST=127.0.0.1
DB_PORT=27017
DB_DATABASE=EmployeeDb
DB_USERNAME=
DB_PASSWORD=

  • the database.php file

1
2
3
4
5
6
7
8
9

'mongodb' => [
            'driver'   => 'mongodb',
            'host'     => env['MONGO_DB_HOST', 'localhost'],
            'port'     => env['MONGO_DB_PORT', 27017],
            'database' => env['MONGO_DB_DATABASE','EmployeeDb'],
            'username' => env['MONGO_DB_USERNAME'],
            'password' => env['MONGO_DB_PASSWORD'],
            'options'  => []
        ],

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
2
3

'providers' => [
    Jenssegers\Mongodb\MongodbServiceProvider::class,
    ]

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Chủ Đề