{"id":3015,"date":"2020-07-29T10:34:37","date_gmt":"2020-07-29T10:34:37","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=3015"},"modified":"2020-07-29T10:38:33","modified_gmt":"2020-07-29T10:38:33","slug":"how-to-use-mongodb-with-laravel-eloquent-operations","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/","title":{"rendered":"How to use MongoDB with Laravel Eloquent operations?"},"content":{"rendered":"<h2>Introduction to MongoDB<\/h2>\n<p>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.<br \/>\nConfigure the Database:<\/p>\n<pre>\r\nMONGO_DB_HOST=127.0.0.1\r\nMONGO_DB_PORT=27017\r\nMONGO_DB_DATABASE= mongocrud\r\nMONGO_DB_USERNAME=\r\nMONGO_DB_PASSWORD=\r\n<\/pre>\n<p>Add new connection in config>>database.php<br \/>\n<code>'connections' => [<\/p>\n<p>        ...<br \/>\n     'mongodb' => [<br \/>\n            'driver'   => 'mongodb',<br \/>\n            'host'     => env('MONGO_DB_HOST', 'localhost'),<br \/>\n            'port'     => env('MONGO_DB_PORT', 27017),<br \/>\n            'database' => env('MONGO_DB_DATABASE'),<br \/>\n            'username' => env('MONGO_DB_USERNAME'),<br \/>\n            'password' => env('MONGO_DB_PASSWORD'),<br \/>\n            'options'  => []\n        ],<br \/>\n    ]<\/code><\/p>\n<h3>Laravel version Compatibility<\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>Laravel<\/b><\/td>\n<td><b>Package<\/b><\/td>\n<\/tr>\n<tr>\n<td>4.2.x<\/td>\n<td>2.0.x<\/td>\n<\/tr>\n<tr>\n<td>5.0.x<\/td>\n<td>2.1.x<\/td>\n<\/tr>\n<tr>\n<td>5.1.x<\/td>\n<td>2.2.x or 3.0.x<\/td>\n<\/tr>\n<tr>\n<td>5.2.x<\/td>\n<td>2.3.x or 3.0.x<\/td>\n<\/tr>\n<tr>\n<td>5.3.x<\/td>\n<td>3.1.x or 3.2.x<\/td>\n<\/tr>\n<tr>\n<td>5.4.x<\/td>\n<td>3.2.x<\/td>\n<\/tr>\n<tr>\n<td>5.5.x<\/td>\n<td>3.3.x<\/td>\n<\/tr>\n<tr>\n<td>5.6.x<\/td>\n<td>3.4.x<\/td>\n<\/tr>\n<tr>\n<td>5.7.x<\/td>\n<td>3.4.x<\/td>\n<\/tr>\n<tr>\n<td>5.8.x<\/td>\n<td>3.5.x<\/td>\n<\/tr>\n<tr>\n<td>6.0.x<\/td>\n<td>3.6.x<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Installation:<\/h3>\n<p>Before the installation of this package, make sure you have Mongo DB PHP driver installed on your system.<br \/>\n<code>composer require jenssegers\/mongodb<\/code><\/p>\n<p>If you want to install a specific version :<br \/>\n<code>composer require jenssegers\/mongodb:3.4.x<\/code><\/p>\n<p>add the service provider in config\/app.php:<br \/>\n<code>'providers' => [<br \/>\n        Jenssegers\\Mongodb\\MongodbServiceProvider::class,<br \/>\n           ]<\/code><\/p>\n<p><strong>Example:<\/strong><br \/>\nCreate a model class<br \/>\n<code>Php artisan make:model User<\/code><\/p>\n<p>User model will look like this:<br \/>\n<code>&lt;?php<\/p>\n<p>namespace App;<\/p>\n<p>use Illuminate\\Database\\Eloquent\\Model;<br \/>\nuse Jenssegers\\Mongodb\\Eloquent\\Model as Eloquent;<\/p>\n<p>class User extends Eloquent<br \/>\n{<br \/>\n    protected $connection = 'mongodb'; \/\/specifies which connection you want to use for the model<br \/>\n    protected $collection = \u2018users\u2019;<\/p>\n<p>    protected $fillable = [<br \/>\n        'name', 'email','address'<br \/>\n    ];<br \/>\n}<\/code><\/p>\n<p>Make route in routes>>api.php<br \/>\n<code>Route::post('allUsers','UserController@users);<\/code><\/p>\n<p>Now create controller and add following code in controller:<br \/>\n<code>php artisan make:controller UserController<\/code><\/p>\n<p><code>use App\\User;<br \/>\n   public function users(Request $request)<br \/>\n    {<br \/>\n        $user = User::select(\u2018name\u2019,\u2019email\u2019,\u2019address\u2019)->get();<br \/>\n        \/\/it will work exactly the same as that of the laravel basic eloquent queries.<br \/>\n        if($user){<br \/>\n        echo json_encode(array('success'=>1,\u2019data\u2019=>$user,'message'=>'success'));<br \/>\n      else{<br \/>\n        echo json_encode(array('success'=>0,\u2019data\u2019=>[],'message'=>'failure'));<\/p>\n<p>        }<br \/>\n      }<br \/>\n    }<\/code><\/p>\n<p>If you type the URL: http:\/\/localhost:8000\/api\/allUsers you will get the response.<\/p>\n<p><small>People having good command over the French language can get a French certification from StudySection. StudySection offers both beginner level and expert level <a href=\"https:\/\/www.studysection.com\/basics-of-french-foundation\">French certification exams<\/a> to test the ability to communicate in the French language.<br \/>\n<\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to MongoDB MongoDB is an open-source cross-platform and document-oriented NoSQL database that is used for large volume data storage.<\/p>\n","protected":false},"author":1,"featured_media":3023,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[503,65,502],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to use MongoDB with Laravel Eloquent operations? - SS Blog<\/title>\n<meta name=\"description\" content=\"MongoDB is an open-source cross-platform and document-oriented NoSQL database that is used for large volume data storage.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use MongoDB with Laravel Eloquent operations? - SS Blog\" \/>\n<meta property=\"og:description\" content=\"MongoDB is an open-source cross-platform and document-oriented NoSQL database that is used for large volume data storage.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-29T10:34:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-07-29T10:38:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/07\/laravel-and-mongodb-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"2228\" \/>\n\t<meta property=\"og:image:height\" content=\"1248\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"How to use MongoDB with Laravel Eloquent operations?\",\"datePublished\":\"2020-07-29T10:34:37+00:00\",\"dateModified\":\"2020-07-29T10:38:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/\"},\"wordCount\":214,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"Eloquent\",\"laravel\",\"MongoDB\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/\",\"url\":\"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/\",\"name\":\"How to use MongoDB with Laravel Eloquent operations? - SS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2020-07-29T10:34:37+00:00\",\"dateModified\":\"2020-07-29T10:38:33+00:00\",\"description\":\"MongoDB is an open-source cross-platform and document-oriented NoSQL database that is used for large volume data storage.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to use MongoDB with Laravel Eloquent operations?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to use MongoDB with Laravel Eloquent operations? - SS Blog","description":"MongoDB is an open-source cross-platform and document-oriented NoSQL database that is used for large volume data storage.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/","og_locale":"en_US","og_type":"article","og_title":"How to use MongoDB with Laravel Eloquent operations? - SS Blog","og_description":"MongoDB is an open-source cross-platform and document-oriented NoSQL database that is used for large volume data storage.","og_url":"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2020-07-29T10:34:37+00:00","article_modified_time":"2020-07-29T10:38:33+00:00","og_image":[{"width":2228,"height":1248,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/07\/laravel-and-mongodb-1.jpg","type":"image\/jpeg"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"How to use MongoDB with Laravel Eloquent operations?","datePublished":"2020-07-29T10:34:37+00:00","dateModified":"2020-07-29T10:38:33+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/"},"wordCount":214,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["Eloquent","laravel","MongoDB"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/","url":"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/","name":"How to use MongoDB with Laravel Eloquent operations? - SS Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2020-07-29T10:34:37+00:00","dateModified":"2020-07-29T10:38:33+00:00","description":"MongoDB is an open-source cross-platform and document-oriented NoSQL database that is used for large volume data storage.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to use MongoDB with Laravel Eloquent operations?"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":2779,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3015"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=3015"}],"version-history":[{"count":8,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3015\/revisions"}],"predecessor-version":[{"id":3025,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3015\/revisions\/3025"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/3023"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=3015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=3015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=3015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}