<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>How to configure multiple databases in Laravel Archives - Artificial Intelligence</title>
	<atom:link href="https://www.aiuniverse.xyz/tag/how-to-configure-multiple-databases-in-laravel/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.aiuniverse.xyz/tag/how-to-configure-multiple-databases-in-laravel/</link>
	<description>Exploring the universe of Intelligence</description>
	<lastBuildDate>Fri, 13 Sep 2024 11:26:36 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Laravel Multi-Database Configuration: Best Practices and Setup Guide</title>
		<link>https://www.aiuniverse.xyz/laravel-multi-database-configuration-best-practices-and-setup-guide/</link>
					<comments>https://www.aiuniverse.xyz/laravel-multi-database-configuration-best-practices-and-setup-guide/#respond</comments>
		
		<dc:creator><![CDATA[Maruti Kr.]]></dc:creator>
		<pubDate>Fri, 13 Sep 2024 11:26:17 +0000</pubDate>
				<category><![CDATA[laravel]]></category>
		<category><![CDATA[Advanced Laravel database configuration]]></category>
		<category><![CDATA[Handling multiple databases in Laravel]]></category>
		<category><![CDATA[How to configure multiple databases in Laravel]]></category>
		<category><![CDATA[Laravel database connection tutorial]]></category>
		<category><![CDATA[Laravel database management]]></category>
		<category><![CDATA[Laravel Database Setup]]></category>
		<category><![CDATA[Laravel multi-database configuration]]></category>
		<category><![CDATA[Laravel multi-DB connections]]></category>
		<category><![CDATA[Multiple database connections Laravel]]></category>
		<category><![CDATA[Multiple databases in Laravel framework]]></category>
		<guid isPermaLink="false">https://www.aiuniverse.xyz/?p=19128</guid>

					<description><![CDATA[<p>To configure multiple databases in Laravel, you&#8217;ll need to modify your .env file as well as the config/database.php file to manage the connections. Step 1: Update the <a class="read-more-link" href="https://www.aiuniverse.xyz/laravel-multi-database-configuration-best-practices-and-setup-guide/">Read More</a></p>
<p>The post <a href="https://www.aiuniverse.xyz/laravel-multi-database-configuration-best-practices-and-setup-guide/">Laravel Multi-Database Configuration: Best Practices and Setup Guide</a> appeared first on <a href="https://www.aiuniverse.xyz">Artificial Intelligence</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="1024" height="1024" src="https://www.aiuniverse.xyz/wp-content/uploads/2024/09/DALL·E-2024-09-13-16.53.09-An-abstract-illustration-depicting-Laravel-framework-with-two-separate-database-icons-connected-to-it.-The-Laravel-logo-is-in-the-center-with-arrows-.webp" alt="" class="wp-image-19129" srcset="https://www.aiuniverse.xyz/wp-content/uploads/2024/09/DALL·E-2024-09-13-16.53.09-An-abstract-illustration-depicting-Laravel-framework-with-two-separate-database-icons-connected-to-it.-The-Laravel-logo-is-in-the-center-with-arrows-.webp 1024w, https://www.aiuniverse.xyz/wp-content/uploads/2024/09/DALL·E-2024-09-13-16.53.09-An-abstract-illustration-depicting-Laravel-framework-with-two-separate-database-icons-connected-to-it.-The-Laravel-logo-is-in-the-center-with-arrows--300x300.webp 300w, https://www.aiuniverse.xyz/wp-content/uploads/2024/09/DALL·E-2024-09-13-16.53.09-An-abstract-illustration-depicting-Laravel-framework-with-two-separate-database-icons-connected-to-it.-The-Laravel-logo-is-in-the-center-with-arrows--150x150.webp 150w, https://www.aiuniverse.xyz/wp-content/uploads/2024/09/DALL·E-2024-09-13-16.53.09-An-abstract-illustration-depicting-Laravel-framework-with-two-separate-database-icons-connected-to-it.-The-Laravel-logo-is-in-the-center-with-arrows--768x768.webp 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>To configure multiple databases in Laravel, you&#8217;ll need to modify your <code><strong>.env</strong></code> file as well as the <code><strong>config/database.php</strong></code> file to manage the connections.</p>



<h3 class="wp-block-heading">Step 1: Update the <code>.env</code> file</h3>



<p>In your <code><strong>.env</strong></code> file, define the settings for both databases. You can have a default connection as well as a second connection with a new prefix.</p>



<p>Example <code><strong>.env</strong></code> file:</p>



<pre class="wp-block-code"><code>DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=default_database
DB_USERNAME=root
DB_PASSWORD=password

DB_SECOND_CONNECTION=mysql
DB_SECOND_HOST=127.0.0.1
DB_SECOND_PORT=3306
DB_SECOND_DATABASE=second_database
DB_SECOND_USERNAME=root
DB_SECOND_PASSWORD=password</code></pre>



<h3 class="wp-block-heading">Step 2: Configure <code>config/database.php</code></h3>



<p>Now, go to the <code><strong>config/database.php</strong></code> file and configure the connections. Laravel uses the default connection for the database interactions unless specified otherwise. To add multiple database connections, follow these steps:</p>



<p>In the <code>connections</code> array inside <code><strong>config/database.php</strong></code>, add a new connection for the second database like this:</p>



<pre class="wp-block-code"><code>'connections' =&gt; &#91;

    'mysql' =&gt; &#91;
        'driver' =&gt; 'mysql',
        'host' =&gt; env('DB_HOST', '127.0.0.1'),
        'port' =&gt; env('DB_PORT', '3306'),
        'database' =&gt; env('DB_DATABASE', 'forge'),
        'username' =&gt; env('DB_USERNAME', 'forge'),
        'password' =&gt; env('DB_PASSWORD', ''),
        'unix_socket' =&gt; env('DB_SOCKET', ''),
        'charset' =&gt; 'utf8mb4',
        'collation' =&gt; 'utf8mb4_unicode_ci',
        'prefix' =&gt; '',
        'strict' =&gt; true,
        'engine' =&gt; null,
    ],

    'mysql2' =&gt; &#91;
        'driver' =&gt; 'mysql',
        'host' =&gt; env('DB_SECOND_HOST', '127.0.0.1'),
        'port' =&gt; env('DB_SECOND_PORT', '3306'),
        'database' =&gt; env('DB_SECOND_DATABASE', 'forge'),
        'username' =&gt; env('DB_SECOND_USERNAME', 'forge'),
        'password' =&gt; env('DB_SECOND_PASSWORD', ''),
        'unix_socket' =&gt; env('DB_SECOND_SOCKET', ''),
        'charset' =&gt; 'utf8mb4',
        'collation' =&gt; 'utf8mb4_unicode_ci',
        'prefix' =&gt; '',
        'strict' =&gt; true,
        'engine' =&gt; null,
    ],
],</code></pre>



<h3 class="wp-block-heading">Step 3: Using Multiple Databases in Your Code</h3>



<p>Now that you have multiple connections, you can specify which connection to use in your models, queries, or migrations.</p>



<p>For example, if you want to use the second database connection in a query:</p>



<pre class="wp-block-code"><code>$users = DB::connection('mysql2')-&gt;select('SELECT * FROM users');</code></pre>



<p>Or if you want to specify a connection for a particular model:</p>



<pre class="wp-block-code"><code>class User extends Model
{
    protected $connection = 'mysql2';  // Use the second database connection
}</code></pre>



<h3 class="wp-block-heading">Step 4: Running Migrations</h3>



<p>To run migrations for different connections, you can specify the connection like this:</p>



<pre class="wp-block-code"><code>php artisan migrate --database=mysql2</code></pre>



<p>This will run the migrations on the second database connection.</p>



<p>By following these steps, you can easily configure and use multiple databases in Laravel.</p>
<p>The post <a href="https://www.aiuniverse.xyz/laravel-multi-database-configuration-best-practices-and-setup-guide/">Laravel Multi-Database Configuration: Best Practices and Setup Guide</a> appeared first on <a href="https://www.aiuniverse.xyz">Artificial Intelligence</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.aiuniverse.xyz/laravel-multi-database-configuration-best-practices-and-setup-guide/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
