{"id":3176,"date":"2020-08-24T04:33:51","date_gmt":"2020-08-24T04:33:51","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=3176"},"modified":"2020-08-24T06:50:44","modified_gmt":"2020-08-24T06:50:44","slug":"how-to-use-two-databases-in-a-single-project","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/","title":{"rendered":"How to Use Two Databases In a Single Project?"},"content":{"rendered":"<p>We will be discussing the usage of two databases (MySQL and MongoDB) in one <a href=\"https:\/\/studysection.com\/blog\/deploying-laravel-project-on-heroku-server\/\">project<\/a> (Laravel).<\/p>\n<ol>\n<li>\n<h2>For multiple databases first of all, go to your .env file and set the following:<\/h2>\n<p><code><br \/>\nDB_CONNECTION=mysql<br \/>\nDB_HOST=localhost<br \/>\nDB_PORT=3306<br \/>\nDB_DATABASE=database1<br \/>\nDB_USERNAME=root<br \/>\nDB_PASSWORD=123456<\/p>\n<p>DB_CONNECTION_SECOND=mongodb<br \/>\nMONGO_DB_HOST=localhost<br \/>\nMONGO_DB_PORT=27017<br \/>\nMONGO_DB_DATABASE=database2<br \/>\nMONGO_DB_USERNAME=<strong>null<\/strong><br \/>\nMONGO_DB_PASSWORD=<strong>null<\/strong><br \/>\n<\/code>\n<\/li>\n<li>\n<h3>After changes in .env file go to your config\/database.php<\/h3>\n<p><code><br \/>\n'mysql' => [<br \/>\n'driver' => env('DB_CONNECTION'),<br \/>\n'host' => env('DB_HOST'),<br \/>\n'port' => env('DB_PORT'),<br \/>\n'database' => env('DB_DATABASE'),<br \/>\n'username' => env('DB_USERNAME'),<br \/>\n'password' => env('DB_PASSWORD'),<br \/>\n],<\/p>\n<p>\t'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>\n<\/li>\n<li>\n<h3>When you are creating a migration use database like this<\/h3>\n<p><code><br \/>\n public function up()<br \/>\n    {<br \/>\n        Schema::connection('mongodb')->create('table_name', function (Blueprint $table) {<br \/>\n            $table->increments('id');<br \/>\n            $table->string('user_name');<br \/>\n            $table->timestamps();<br \/>\n        });<br \/>\n    }<\/p>\n<p><\/code>\n<\/li>\n<li>\n<h3>If you are using eloquent then create a model and add these<\/h3>\n<p><code><br \/>\n    <strong>protected<\/strong> $connection = 'mongodb';<br \/>\n    <strong>protected <\/strong>$collection = 'table_name';<br \/>\n<\/code><br \/>\nIn your class. Now you can use your model where you want to like you use your normal models.<br \/>\nFor example:<\/p>\n<p><code>$data = TableName:get()->toArray();<\/code>\n<\/li>\n<li>\n<h3>If you are not using eloquent then use query builder like this<\/h3>\n<p><code>$data = DB::connection('mysql2')->select(...);<\/code>\n<\/li>\n<\/ol>\n<p><small><em>jQuery presents a tree-like structure of all the elements on a webpage simplifying the syntax and further manipulating such elements. The <a href=\"https:\/\/www.studysection.com\/jquery-3.x-advanced\">jQuery Certification exam<\/a> by StudySection will secure your fundamental knowledge and a basic understanding of jQuery as an asset to improve your skills.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We will be discussing the usage of two databases (MySQL and MongoDB) in one project (Laravel). For multiple databases first<\/p>\n","protected":false},"author":1,"featured_media":3179,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[525,306],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>StudySection Blog - How to use two databases in a single project<\/title>\n<meta name=\"description\" content=\"We will be discussing the usage of two databases (Structured Query Language and MongoDB) in a single project (in Laravel).\" \/>\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-two-databases-in-a-single-project\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"StudySection Blog - How to use two databases in a single project\" \/>\n<meta property=\"og:description\" content=\"We will be discussing the usage of two databases (Structured Query Language and MongoDB) in a single project (in Laravel).\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/\" \/>\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-08-24T04:33:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-24T06:50:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/08\/db.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\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=\"1 minute\" \/>\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-two-databases-in-a-single-project\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"How to Use Two Databases In a Single Project?\",\"datePublished\":\"2020-08-24T04:33:51+00:00\",\"dateModified\":\"2020-08-24T06:50:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/\"},\"wordCount\":150,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"Databases\",\"SQL\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/\",\"url\":\"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/\",\"name\":\"StudySection Blog - How to use two databases in a single project\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2020-08-24T04:33:51+00:00\",\"dateModified\":\"2020-08-24T06:50:44+00:00\",\"description\":\"We will be discussing the usage of two databases (Structured Query Language and MongoDB) in a single project (in Laravel).\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Use Two Databases In a Single Project?\"}]},{\"@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":"StudySection Blog - How to use two databases in a single project","description":"We will be discussing the usage of two databases (Structured Query Language and MongoDB) in a single project (in Laravel).","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-two-databases-in-a-single-project\/","og_locale":"en_US","og_type":"article","og_title":"StudySection Blog - How to use two databases in a single project","og_description":"We will be discussing the usage of two databases (Structured Query Language and MongoDB) in a single project (in Laravel).","og_url":"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2020-08-24T04:33:51+00:00","article_modified_time":"2020-08-24T06:50:44+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/08\/db.png","type":"image\/png"}],"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":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"How to Use Two Databases In a Single Project?","datePublished":"2020-08-24T04:33:51+00:00","dateModified":"2020-08-24T06:50:44+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/"},"wordCount":150,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["Databases","SQL"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/","url":"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/","name":"StudySection Blog - How to use two databases in a single project","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2020-08-24T04:33:51+00:00","dateModified":"2020-08-24T06:50:44+00:00","description":"We will be discussing the usage of two databases (Structured Query Language and MongoDB) in a single project (in Laravel).","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/how-to-use-two-databases-in-a-single-project\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Use Two Databases In a Single Project?"}]},{"@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":1494,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3176"}],"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=3176"}],"version-history":[{"count":4,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3176\/revisions"}],"predecessor-version":[{"id":3181,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3176\/revisions\/3181"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/3179"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=3176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=3176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=3176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}