我已经使用Laravel迁移向数据库表添加了一个新列,如下所示:
<?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');
});
}
}作为FAQ的默认值,我希望在运行迁移时为所有商店预先填充以下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>是否可以添加一个新列,同时使用类似于上面的HTML块预先填充它?如果使用数据库种子是一种更好的做法,请也给出建议。谢谢
发布于 2021-06-04 13:17:15
我建议不要使用这种方法,在你的数据库中存储一个简单的段落,并将其添加到具有所需标签的刀片模板中,但如果你坚持并想要插入默认数据,如FAQ或其他没有特定结构的东西,你应该使用Laravel Query Builder添加它们,如下所示:
// 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>'));并在刀片式模板中使用它。
发布于 2021-06-04 13:19:40
您可以使用Seeder和执行查询来更新新列。1.参阅- Link 2.在迁移文件中执行查询
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);
}https://stackoverflow.com/questions/67831540
复制相似问题