pecl install mongodbStep 01
Install MongoDB PHP driver from https://pecl.php.net/package/mongodbMake sure you use the correct version compatible with both your PHP version and also driver version
Note : Make sure you choose the correct OS .. for example for 64bit you must use x64 edition. ThreadSafe one
Step 02
Extract the folder and Copy the php_mongo.dll from the extracted and paste into php extension folderC:\xampp\php\ext
Step 03
Now you need to add the php extension. Open php.ini file under C:\xampp\php\ and add the following to the extension groupextension=php_mongo.dll
Then restart your xampp and check phpinfo
If you're working on Linux
Install Mongo DB PHP driver
sudo apt-get install php5-dev php5-cli php-pear
sudo pecl install mongoor
pecl install mongodb
orsudo apt-get install php5-mongo (For PHP 5)
sudo apt-get install php-mongodb (for PHP 5.6 or higher)
Building Mongodb driver Manually if the above approach doesn;t work
sudo apt-get install build-essential
apt-get install autoconf
apt-get update
sudo apt-get install libpcre3-devpecl install mongodbAdd the following line in php.ini fileextension=mongo.so for (PHP 5.5 or lower)if you're using nano, you can search using Ctrl + Wextension=mongodb.so for (PHP 5.6 or higher)To check the path to php.ini file,php -i | grep 'Configuration File'and to see mongo is enabledphp -i | grep 'Mongo'
Configuring Laravel Database to use MongoDB
Modify the database.php under Config file in laravel file structure.
'default' => env('DB_CONNECTION', 'mongodb'),
'mongodb' => [ 'driver' => 'mongodb', 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', 27017), 'database' => 'eshop', 'username' => '', 'password' => '',// 'username' => env('DB_USERNAME', ''),// 'password' => env('DB_PASSWORD', ''), ' 'charset' => 'utf8', 'prefix' => '', ],
Create the following php file and then run it, you'll be able to see a new database<?php$user = array('first_name' => 'MongoDB','last_name' => 'Fan','tags' => array('developer','user')); // Configuration$dbhost = 'localhost';$dbname = 'testmongo'; // Connect to test database$m = new MongoClient("mongodb://$dbhost");$db = $m->$dbname; // Get the users collection$c_users = $db->users; // Insert this new document into the users collection$c_users->save($user); ?>Now you can install the jenssengers to connect to MongoDB with ORM supportInside your project root folder, right click and Run Composer here. Then install jenssengers by typing the following composer require jenssegers/mongodb
After the jenssengers installation is done add the followig line inside app.php to start using jenssengers
Jenssegers\Mongodb\MongodbServiceProvider::class, //Mongodb service provider
Now if you want to use model class with your MongodB and not the default one, you should always use the following sample
<?php namespace Furbook;//use Illuminate\Database\Eloquent\Model;use Jenssegers\Mongodb\Model as Eloquent; //Use MongoDB as a model
class Breed extends Eloquent//class Breed extends Model{ public $timestamps = false; public function cats(){ return $this->hasMany('Furbook\Cat'); }}
You can also include HTML form and views by including the following line in the composer.json required field and run composer update to start using it
"laravelcollective/html": "5.0.*"
and make sure that you're running php artisan db:seed from the project root folder.
No comments:
Post a Comment