Laravel - Model properties remain NULL in database even after creation
P粉015402013
P粉015402013 2024-02-25 19:07:40
0
2
326

I have a model called Articles which contains three properties: 'title', 'subtitle' and 'body', it works perfectly but four columns are added to the model ('subtitle2' , 'body2', 'subtitle3' and 'body3'), the newly added column still remains empty after creating the article. Clearly I'm missing something, but I can't figure out what.

This is the migration:

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

After the migration, I edited app/Http/Models/Article.php and it looks like this:

protected $fillable = [
    'title',
    'subtitle',
    'body',
    'subtitle2',
    'body2',
    'subtitle3',
    'body3',

];

This is my 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));
            }
            
        }          
}

Finally, I added these lines to the form:

{{-- 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); Returns the following content

Tinker displays all columns

P粉015402013
P粉015402013

reply all(2)
P粉342101652

Assume that the dd(); image you provide is up to date. I can see that the new column does not exist in the database. ('subtitle2', 'body2', 'subtitle3' and 'body3') All of these are not available in the list. So I think you forgot to run the migration command

php artisan migrate
P粉864872812

You need to specify

protected $rules

can be used

$this->validate()
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!