我有一个名为Articles的模型,其中包含三个属性:'title','subtitle'和'body',它工作得很完美,但在该模型中添加了四个列('subtitle2','body2','subtitle3'和'body3')后,新添加的列在创建文章后仍然保持为空。 显然我错过了什么,但我无法弄清楚是什么。
这是迁移:
public function up() { Schema::table('articles', function (Blueprint $table) { $table->string('subtitle2')->nullable()->default(null); $table->text('body2')->nullable()->default(null); $table->string('subtitle3')->nullable()->default(null); $table->text('body3')->nullable()->default(null); }); }
迁移后,我编辑了app / Http / Models / Article.php,它如下所示:
protected $fillable = [ 'title', 'subtitle', 'body', 'subtitle2', 'body2', 'subtitle3', 'body3', ];
这是我的app / Http / Livewire / CreateArticle.php
class CreateArticle extends Component { use WithFileUploads; public $title; public $subtitle; public $body; public $category; public $subtitle2; public $body2; public $subtitle3; public $body3; public $temporary_images; public $images = []; public $article; public function store() { $this->validate(); $this->article = Category::find($this->category)->articles()->create($this->validate()); $this->article->user()->associate(Auth::user()); $this->article->save(); if(count($this->images)){ foreach($this->images as $image){ $newImage = $this->article->images()->create(['path'=>$image->store('images', 'public')]); dispatch(new ResizeImage($newImage->path, 600, 400)); } } }
最后,我在表单中添加了这些行:
{{-- INSERT SUBTITLE 2 --}} <div class="mb-3"> <label for="subtitle2" class="form-label">第二段副标题</label> <input type="text" wire:model="subtitle2" class="form-control" id="subtitle2"> </div> {{-- INSERT PARAGRAPH 2 --}} <div class="mb-3"> <label for="body2" class="form-label">第二段</label><br> <textarea class="form-control" wire:model="body2" id="body2" cols="30" rows="3"></textarea> </div> {{-- INSERT SUBTITLE 3 --}} <div class="mb-3"> <label for="subtitle3" class="form-label">第三段副标题</label> <input type="text" wire:model="subtitle3" class="form-control" id="subtitle3"> </div> {{-- INSERT PARAGRAPH 3 --}} <div class="mb-3"> <label for="body3" class="form-label">第三段</label><br> <textarea class="form-control" wire:model="body3" id="body3" cols="30" rows="3"></textarea> </div>
dd($this); 返回以下内容
Tinker显示所有列
P粉3421016522024-02-26 15:22:21
假设您提供的dd();图像是最新的。我可以看到数据库中不存在新的列。('subtitle2','body2','subtitle3'和'body3')所有这些在列表中都不可用。 所以我认为您忘记运行迁移命令
php artisan migrate