php - Pre-fill database table column field with HTML using Laravel migration

I have added a new column to a database table using Laravel migration as follows:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddFAQToStoreTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('store', function (Blueprint $table) {
            $table->longText('FAQ')->after('description');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('store', function (Blueprint $table) {
            $table->dropColumn('FAQ');
        });
    }
}

As a default value for FAQ, I want the following HTML pre-filled for all stores when I run the migration:

<div><span style="font-weight:600">Can we get our purchase delivered?</span><br/>
Yes, items purchased can be delivered. However, due to COVID-19 restrictions, we are expecting a 3-5 business days' delay.</div>

Is it possible to add a new column and simultaneously pre-populate it with a HTML block like above? If it's better practice to use database seeders, please advise as well. Thanks

Answer

Solution:

You can update new column using Seeder and executing query. 1.Seeder - Link 2. executing query in your migration file

 public function up()
    {
         Schema::table('store', function (Blueprint $table) {
             $table->longText('FAQ')->after('description');
         });
         $html = '<div><span style="font-weight:600">Can we get our purchase delivered?</span><br/>
Yes, items purchased can be delivered. However, due to COVID-19 restrictions, we are expecting a 3-5 business days' delay.</div>';
         DB::statement('UPDATE store SET FAQ='.$html);
    }

Answer

Solution:

I would recommend don't use this method, store a simple paragraph in your DB and add it to your blade template with the desired tag but If you insist and want to insert default data such as FAQ or other things, which don't have a specific structure, you should add them with Laravel Query Builder like below:

// define table

   public function up()
    {
        Schema::table('store', function (Blueprint $table) {
            $table->longText('FAQ')->after('description');
        });
    }


// insert some stuff

    DB::table('store')->where('FAQ', '=', '')->update(array('FAQ' => '<div> . . . </div>'));

And use it in your blade template.

Source