Author - StudySection Post Views - 2,658 views
laravel eloquent mongodb

How to use MongoDB with Laravel Eloquent operations?

Introduction to MongoDB

MongoDB is an open-source cross-platform and document-oriented NoSQL database that is used for large volume data storage. Laravel provides a package which deals with the eloquent operations for MongoDB. The library we are using extends the original Laravel classes, so it can use exactly the same methods and operations.
Configure the Database:

MONGO_DB_HOST=127.0.0.1
MONGO_DB_PORT=27017
MONGO_DB_DATABASE= mongocrud
MONGO_DB_USERNAME=
MONGO_DB_PASSWORD=

Add new connection in config>>database.php
'connections' => [

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

Laravel version Compatibility

Laravel Package
4.2.x 2.0.x
5.0.x 2.1.x
5.1.x 2.2.x or 3.0.x
5.2.x 2.3.x or 3.0.x
5.3.x 3.1.x or 3.2.x
5.4.x 3.2.x
5.5.x 3.3.x
5.6.x 3.4.x
5.7.x 3.4.x
5.8.x 3.5.x
6.0.x 3.6.x

Installation:

Before the installation of this package, make sure you have Mongo DB PHP driver installed on your system.
composer require jenssegers/mongodb

If you want to install a specific version :
composer require jenssegers/mongodb:3.4.x

add the service provider in config/app.php:
'providers' => [
Jenssegers\Mongodb\MongodbServiceProvider::class,
]

Example:
Create a model class
Php artisan make:model User

User model will look like this:
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class User extends Eloquent
{
protected $connection = 'mongodb'; //specifies which connection you want to use for the model
protected $collection = ‘users’;

protected $fillable = [
'name', 'email','address'
];
}

Make route in routes>>api.php
Route::post('allUsers','UserController@users);

Now create controller and add following code in controller:
php artisan make:controller UserController

use App\User;
public function users(Request $request)
{
$user = User::select(‘name’,’email’,’address’)->get();
//it will work exactly the same as that of the laravel basic eloquent queries.
if($user){
echo json_encode(array('success'=>1,’data’=>$user,'message'=>'success'));
else{
echo json_encode(array('success'=>0,’data’=>[],'message'=>'failure'));

}
}
}

If you type the URL: http://localhost:8000/api/allUsers you will get the response.

People having good command over the French language can get a French certification from StudySection. StudySection offers both beginner level and expert level French certification exams to test the ability to communicate in the French language.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri