<?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>Laravel .env database setup Archives - Artificial Intelligence</title>
	<atom:link href="https://www.aiuniverse.xyz/tag/laravel-env-database-setup/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.aiuniverse.xyz/tag/laravel-env-database-setup/</link>
	<description>Exploring the universe of Intelligence</description>
	<lastBuildDate>Sat, 03 Aug 2024 10:15:50 +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>Database configuration &#038; migration in Laravel</title>
		<link>https://www.aiuniverse.xyz/database-configuration-migration-in-laravel/</link>
					<comments>https://www.aiuniverse.xyz/database-configuration-migration-in-laravel/#respond</comments>
		
		<dc:creator><![CDATA[Maruti Kr.]]></dc:creator>
		<pubDate>Sat, 03 Aug 2024 10:15:48 +0000</pubDate>
				<category><![CDATA[laravel]]></category>
		<category><![CDATA[Create migrations Laravel]]></category>
		<category><![CDATA[How to set up Laravel database]]></category>
		<category><![CDATA[Laravel .env database setup]]></category>
		<category><![CDATA[Laravel database configuration]]></category>
		<category><![CDATA[Laravel database seeding]]></category>
		<category><![CDATA[Laravel migration rollback]]></category>
		<category><![CDATA[Laravel Migration Tutorial]]></category>
		<category><![CDATA[Laravel Schema Builder]]></category>
		<category><![CDATA[PHP artisan migrate command]]></category>
		<guid isPermaLink="false">https://www.aiuniverse.xyz/?p=19002</guid>

					<description><![CDATA[<p>In Laravel, database configuration and migration are fundamental tasks for setting up and managing your application&#8217;s database. Here’s how you can handle both: 1. Database Configuration Before <a class="read-more-link" href="https://www.aiuniverse.xyz/database-configuration-migration-in-laravel/">Read More</a></p>
<p>The post <a href="https://www.aiuniverse.xyz/database-configuration-migration-in-laravel/">Database configuration &amp; migration in Laravel</a> appeared first on <a href="https://www.aiuniverse.xyz">Artificial Intelligence</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In Laravel, database configuration and migration are fundamental tasks for setting up and managing your application&#8217;s database. Here’s how you can handle both:</p>



<h3 class="wp-block-heading">1. Database Configuration</h3>



<p>Before you can run migrations, you need to configure your database in Laravel. This is done in the <code>.env</code> file located in your project’s root directory. Here’s a typical configuration for a MySQL database:</p>



<pre class="wp-block-code"><code>DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password</code></pre>



<p>Make sure to replace <code>your_database_name</code>, <code>your_database_username</code>, and <code>your_database_password</code> with your actual database details. Laravel supports other databases like PostgreSQL, SQLite, and SQL Server as well.</p>



<h3 class="wp-block-heading">2. Migration Basics</h3>



<p>Migrations are like version control for your database, allowing you to modify and share the application’s database schema. You can generate a new migration file using the Artisan command line tool provided by Laravel.</p>



<h4 class="wp-block-heading">Creating a Migration</h4>



<p>To create a migration, use the <code>make:migration</code> command:</p>



<pre class="wp-block-code"><code>php artisan make:migration create_users_table --create=users</code></pre>



<p>This command creates a new migration for a <code>users</code> table. The migration file will be located in the <code>database/migrations</code> directory.</p>



<h4 class="wp-block-heading">Migration Structure</h4>



<p>A migration file contains two methods: <code>up()</code> and <code>down()</code>. <code>up()</code> is used to add new tables, columns, or indexes to your database, while <code>down()</code> should reverse the operations performed by the <code>up()</code> method.</p>



<pre class="wp-block-code"><code>public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table-&gt;id();
        $table-&gt;string('name');
        $table-&gt;string('email')-&gt;unique();
        $table-&gt;timestamp('email_verified_at')-&gt;nullable();
        $table-&gt;string('password');
        $table-&gt;rememberToken();
        $table-&gt;timestamps();
    });
}

public function down()
{
    Schema::dropIfExists('users');
}</code></pre>



<h3 class="wp-block-heading">3. Running Migrations</h3>



<p>To apply the migrations, use:</p>



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



<p>This command will execute the <code>up()</code> method in your migrations that haven’t been run yet.</p>



<h3 class="wp-block-heading">4. Rolling Back Migrations</h3>



<p>If you need to undo a migration, you can use:</p>



<pre class="wp-block-code"><code>php artisan migrate:rollback</code></pre>



<p>This command will call the <code>down()</code> method for the last batch of migrations that were applied.</p>



<h3 class="wp-block-heading">5. Database Seeding</h3>



<p>After migrating, you might want to populate your database with initial data. This can be done using seeders.</p>



<h4 class="wp-block-heading">Creating a Seeder</h4>



<p>Generate a seeder via Artisan:</p>



<pre class="wp-block-code"><code>php artisan make:seeder UsersTableSeeder</code></pre>



<p>Then, you can define the data insertion within the <code>run()</code> method of your seeder:</p>



<pre class="wp-block-code"><code>public function run()
{
    DB::table('users')-&gt;insert(&#91;
        'name' =&gt; 'Example User',
        'email' =&gt; 'user@example.com',
        'password' =&gt; Hash::make('password'),
    ]);
}</code></pre>



<h4 class="wp-block-heading">Running Seeders</h4>



<p>Execute the seeder using:</p>



<pre class="wp-block-code"><code>php artisan db:seed --class=UsersTableSeeder</code></pre>



<p>This command populates the <code>users</code> table with the specified data.</p>



<h3 class="wp-block-heading">6. Migration and Seeding Together</h3>



<p>You can also migrate and seed your database in one command:</p>



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



<p>This is helpful for setting up a fresh database with all the necessary schema and data together.</p>
<p>The post <a href="https://www.aiuniverse.xyz/database-configuration-migration-in-laravel/">Database configuration &amp; migration in Laravel</a> appeared first on <a href="https://www.aiuniverse.xyz">Artificial Intelligence</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.aiuniverse.xyz/database-configuration-migration-in-laravel/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
