问题仍然是Laravel 9中添加到现有表的新列无法保存数据
P粉262073176
P粉262073176 2023-08-30 13:15:31
[PHP讨论组]

这是我原来的表格,称为问题:

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,
                
            ]);
}

但是新列没有保存数据,可能是什么错误?

P粉262073176
P粉262073176

全部回复(1)
P粉738046172

您需要将 rules 添加到 Question 模型中的数组 $fillable

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号