【发布时间】:2019-03-27 18:00:32
【问题描述】:
我的数据库中有一个questions 表,其中有一列question_title,现在我添加了一个列question_slug,其中将包含问题的slug URL,那么我如何在 Laravel 迁移中将question_slug 默认值设置为question_title,我需要这一切,因为我在数据库中保存了问题,所以我运行了php artisan make:migration add_column_to_questions --table=questions,现在我有了这个代码:
Schema::table('questions', function (Blueprint $table) {
$table->string('question_slug')->default();
});
【问题讨论】:
-
您首先需要它是唯一的,其次,您必须编写一个脚本来使用
question_text和一些时间戳生成唯一的 url。但是由于您已经在数据库中保存了一些问题,您可以将question_slug添加为最初可以为空的文本并运行您的脚本以基于question_title生成并保存question_slug,然后创建另一个迁移以更新question_slug是唯一的。 -
您想将所有现有的question_title添加到question_slug,还是在创建新的question_title时将slug的默认值作为它?
-
@Thamerbelfkih 不,不,如果有任何方法可以获取问题的标题,那么我将应用此
strtoslug()将其转换为 slug,然后将其保存到变量中,然后将其分配给 @987654334 @... -
你的 strtoslug 是类还是函数?
-
strtoslug是 laravel 的内置函数