Home  >  Q&A  >  body text

Laravel - Model properties remain NULL in database even after creation

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粉015402013P粉015402013260 days ago329

reply all(2)I'll reply

  • P粉342101652

    P粉3421016522024-02-26 15:22:21

    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

    reply
    0
  • P粉864872812

    P粉8648728122024-02-26 11:14:31

    You need to specify

    protected $rules

    can be used

    $this->validate()

    reply
    0
  • Cancelreply