Laravel
How to add new column to existing table without losing data in Laravel
Laravel
<p> In Laravel we will learn how to add new column to existing table without losing data by using migration.<br /> Lets us see an example , we are going to create a migration for blogs.</p>
<ol>
<li>
<p><strong>If you don’t know how to create migration then you can see below command otherwise leave it, and move to second point.</strong></p>
</li>
</ol>
<p><strong> Syntax</strong></p>
<pre><code> php artisan make:migration create_tablename_table</code></pre>
<p><strong> </strong><strong>Example<br /></strong> We will follow the syntax and create migration for blog, after created migration you can see the table. In this table there are two function<strong><code>up()</code> </strong>for run the migration table and <code><strong>down()</strong></code> for reverse migration<strong>. </strong>Now run the below command.</p>
<pre><code> Php artisan make:migration create_blogs_table</code></pre>
<p>After migration you can add table field name, we are adding one field that is <code>“title”.</code></p>
<pre><code>/** Run the migrations */<br />public function up(): void<br />{<br /> Schema::create('blogs', function (Blueprint $table) { <br /> $table->id();<br /> $table->string('title'); <br /> $table->timestamps();<br /> });<br />}<br /><br />/** Reverse the migrations. */<br />public function down(): void<br />{<br /> Schema::dropIfExists('blogs');<br />}</code></pre>
<p>Now run command <code><strong>php artisan migrate</strong></code> and see your table in MySQL database.</p>
<ol start="2">
<li>
<p><strong>Now we want to add new column “slug” in blogs existing table without losing data.</strong></p>
</li>
</ol>
<p>So we have to create another migration for add new column , you can put any name as per your choice so we are creating migrate with <code>“<strong>alter_blog_table” </strong></code>name.</p>
<pre><code><strong>php artisan make:migration create_alter_blogs_table</strong></code></pre>
<p>now you can see your migrated schema blow code in this, we have to add new field “slug” in both <strong><code>up()</code> </strong>and <code><strong>down() </strong></code>function and run artisan</p>
<pre><code> public function up(): void <br /> {<br /> /** here you have add schema with previous table name, in previous table blogs is the table name. */<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;"> Schema::table('blogs', function (Blueprint $table) {<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;"> $table->string('slug'); // add new column HERE<br /></span> });<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;"> }</span><br /><br /> /** Reverse the migrations. */<br /> public function down(): void<br /> {<br /><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;"> Schema::table('blogs', function (Blueprint $table) {<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;"> $table->dropColumn('slug'); // Here you have to add field<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;"> });<br /></span><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;"> }</span></code></pre>
<p>Now you can see again blogs table in your database, new field added successfully without losing data in Laravel.</p>