Skip to content

Instantly share code, notes, and snippets.

@Bhavya8181
Last active August 9, 2023 05:27
Show Gist options
  • Save Bhavya8181/d0d78feb1c8961e1fba149bb96ebf59f to your computer and use it in GitHub Desktop.
Save Bhavya8181/d0d78feb1c8961e1fba149bb96ebf59f to your computer and use it in GitHub Desktop.
Setup Mongodb in Ubantu/Windows & intigrate with Laravel
Reference Link
==============
1. Server connect - https://adevait.com/laravel/using-laravel-with-mongodb
2. Crud - https://www.javatpoint.com/mongodb-crud-in-laravel
3. Basic in local - https://www.geeksforgeeks.org/how-to-install-mongodb-on-laravel-project/
4. Setup mongodb in ubantu - https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/
Install mongodb extention - Windows
===================================
1. Download php_mongodb.dll from pecl mongodb php - latest stable dll
2. Put that DLL to localhost Xampp/php/ext
3. add line to php.ini - extension=php_mongodb.dll
4. restart Xampp server.
Install mongodb extention - Ubantu
===================================
1. Import the public key used by the package management system.
From a terminal, install gnupg and curl if they are not already available:
=> sudo apt-get install gnupg curl
Issue the following command to import the MongoDB public GPG Key from
https://pgp.mongodb.com/server-6.0.asc
=> curl -fsSL https://pgp.mongodb.com/server-6.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg \
--dearmor
2. Create a list file for MongoDB.
Create the list file /etc/apt/sources.list.d/mongodb-org-6.0.list for your version of Ubuntu.
Click on the appropriate tab for your version of Ubuntu. If you are unsure of what Ubuntu version the host is running, open a terminal or shell on the host and execute lsb_release -dc.
Create the /etc/apt/sources.list.d/mongodb-org-6.0.list file for Ubuntu 22.04 (Jammy):
=> echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
3. Reload local package database.
Issue the following command to reload the local package database:
=> sudo apt-get update
4. Install the MongoDB packages.
=> sudo apt-get install php7.4-mongodb
=> sudo apt install php7.4-xml
=> sudo apt-get install php7.4-curl
=> composer require jenssegers/mongodb
5. add below extension in php.ini file /etc/php/7.3/cli/php.ini
extension=mongodb.so
Intigrate IN Laravel Project
============================
1. laravel .nev file setup
----
DB_CONNECTION=mongodb
DB_DATABASE_MONGO=scopegenx
DB_DSN="mongodb+srv://<username>:<password>@dbname_url_server_cluster"
****** mongodb local connection
.env file
-------
MONGO_DB_HOST=127.0.0.1
MONGO_DB_PORT=27017
MONGO_DB_DATABASE=hddatabase
MONGO_DB_USERNAME=
MONGO_DB_PASSWORD=
----
database.php config file
--
'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' => []
],
2. Install dependency lib
composer require jenssegers/mongodb
3. add Service Provider to config/app.php
'providers' => [
....
Jenssegers\Mongodb\MongodbServiceProvider::class,
]
4. Model Changes
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Book extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'books';
.....
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment