Home > Software design >  Laravel - Model attributes stay NULL in database after creation
Laravel - Model attributes stay NULL in database after creation

Time:09-20

I have a model named Articles which contained three attributes: 'title', 'subtitle' and 'body' and it worked perfectly but after adding four columns to that model ('subtitle2', 'body2', 'subtitle3' and 'body3') the newly added columns stay NULL after creating articles. There is clearly something that I missed 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 migrating I edited my 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));
            }
            
        }          
}

And finally I added these lines to the form:

    {{-- INSERT SUBTITLE 2 --}}
    
    <div >
        <label for="subtitle2" >Second paragraph subtitle</label>
        <input type="text" wire:model="subtitle2"  id="subtitle2">
       
    </div>
    
    
    {{-- INSERT PARAGRAPH 2 --}}
    
    <div >
        <label for="body2" >Second paragraph</label><br>
        <textarea  wire:model="body2" id="body2" cols="30" rows="3"></textarea>
    </div>
    
    
    {{-- INSERT SUBTITLE 3 --}}
    
    <div >
        <label for="subtitle3" >Third paragraph subtitle</label>
        <input type="text" wire:model="subtitle3"  id="subtitle3">
        
    </div>
    
    
    {{-- INSERT PARAGRAPH 3 --}}
    
    <div >
        <label for="body3" >Third paragraph</label><br>
        <textarea  wire:model="body3" id="body3" cols="30" rows="3"></textarea>
    </div>

dd($this); is returning the following

Tinker is showing all columns

CodePudding user response:

You need to specify

protected $rules

in order to use

$this->validate()

CodePudding user response:

Assuming the dd(); image you provided is the latest. I can see the new columns does not exists in database. ('subtitle2', 'body2', 'subtitle3' and 'body3') all these are not available in list. so I think you are missing to run the migrate command

php artisan migrate
  • Related