这是我原来的表格,称为问题:
public function up()
{
Schema::create('questions', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('slug');
$table->string('image')->nullable();
$table->string('audio')->nullable();
$table->string('type');
$table->unsignedBigInteger('evaluation_id');
$table->foreign('evaluation_id')->references('id')->on('evaluations')->onDelete('cascade');
$table->timestamps();
});
}
使用此代码,我向现有表添加了一个新列:
php artisan make:migration add_rule_to_questions_table --table=questions php artisan migrate
在新列的迁移文件中,在 up() 方法中添加了以下内容:
public function up()
{
Schema::table('questions', function (Blueprint $table) {
$table->longText('rule')->nullable();
});
}
至此,新列已成功添加到数据库中。但是,当我尝试将数据添加到“问题”表的新列时,数据不会保存在数据库中。
在创建表单中我使用以下代码:
<div class="form-group">
<label>Rules:</label>
<textarea name="rule" id="rule" class="form-control" value="{{old('rule')}}"></textarea>
@error('rule')
<small class="text-danger">{{$message}}</small>
@enderror
</div>
最后在控制器的 store method() 中,我使用以下代码保存数据:
public function store(Request $request){
Question::create([
'title' => $request->title,
'slug' => $request->slug,
'evaluation_id' => $request->evaluation_id,
'type' => "OM",
'rules' => $request->rule,
]);
}
但是新列没有保存数据,可能是什么错误?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
您需要将
rules添加到Question模型中的数组$fillable