Heim  >  Fragen und Antworten  >  Hauptteil

Laravel – Modelleigenschaften bleiben auch nach der Erstellung NULL in der Datenbank

Ich habe ein Modell namens Articles, das drei Eigenschaften enthält: „title“, „subtitle“ und „body“. Es funktioniert einwandfrei, aber dem Modell werden vier Spalten hinzugefügt („subtitle2“, „body2“, „subtitle3“ und „body3“. '), bleiben die neu hinzugefügten Spalten nach dem Erstellen des Artikels leer. Offensichtlich übersehe ich etwas, aber ich kann nicht herausfinden, was.

Das ist die 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);
        
    });
}

Nach der Migration habe ich app/Http/Models/Article.php bearbeitet und es sieht so aus:

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

];

Dies ist meine 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));
            }
            
        }          
}

Schließlich habe ich diese Zeilen zum Formular hinzugefügt:

{{-- 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); gibt den folgenden Inhalt zurück

Tinker zeigt alle Spalten an

P粉015402013P粉015402013211 Tage vor268

Antworte allen(2)Ich werde antworten

  • P粉342101652

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

    假设您提供的dd();图像是最新的。我可以看到数据库中不存在新的列。('subtitle2','body2','subtitle3'和'body3')所有这些在列表中都不可用。 所以我认为您忘记运行迁移命令

    php artisan migrate

    Antwort
    0
  • P粉864872812

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

    您需要指定

    protected $rules

    才能使用

    $this->validate()

    Antwort
    0
  • StornierenAntwort