问题仍然是Laravel 9中添加到现有表的新列无法保存数据
P粉262073176
P粉262073176 2023-08-30 13:15:31
0
1
496
<p>这是我原来的表格,称为问题:</p> <pre class="brush:php;toolbar:false;">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(); }); }</pre> <p>使用此代码,我向现有表添加了一个新列:</p> <pre class="brush:php;toolbar:false;">php artisan make:migration add_rule_to_questions_table --table=questions php artisan migrate</pre> <p>在新列的迁移文件中,在 up() 方法中添加了以下内容:</p> <pre class="brush:php;toolbar:false;">public function up() { Schema::table('questions', function (Blueprint $table) { $table->longText('rule')->nullable(); }); }</pre> <p>至此,新列已成功添加到数据库中。但是,当我尝试将数据添加到“问题”表的新列时,数据不会保存在数据库中。</p> <p>在创建表单中我使用以下代码:</p> <pre class="brush:php;toolbar:false;"><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></pre> <p>最后在控制器的 store method() 中,我使用以下代码保存数据:</p> <pre class="brush:php;toolbar:false;">public function store(Request $request){ Question::create([ 'title' => $request->title, 'slug' => $request->slug, 'evaluation_id' => $request->evaluation_id, 'type' => "OM", 'rules' => $request->rule, ]); }</pre> <p>但是新列没有保存数据,可能是什么错误?</p>
P粉262073176
P粉262073176

全部回复(1)
P粉738046172

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!