LoginSignup
1
1

More than 3 years have passed since last update.

Laravel 【php artisan db:seed】時のエラーに関して

Posted at

Laravel 【php artisan db:seed】を実行した時のエラー処理に関して

【目標】
Laravelで掲示板を作成中に、ダミーデータを生成し、一覧に表示したい

【手順】
seederファイルを下記のコードで生成する

Php artisan make:seeder ファイル名

生成したseederファイルを編集後に、

Php artisan db:seed

で実行したものの、下記のようなエラーが発生した・・・

image.png

Call to undefined method App\Models\Blog::factory()とあるので、Blog フォルダの、blogファイルに問題がありそうなので検索!

【原因】
下記の記事がヒットする、、
https://teratail.com/questions/304871
記事によると、HasFactoryがModels/ blogファイルに抜けている模様。。。

下記コードのように修正

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Blog extends Model
{
    use HasFactory;      ←新たに追加

    protected $table = 'blogs';

    protected $fillable = 
    [
        'title',
        'content'
    ];
}

再度実行すると成功

Php artisan db:seed

Once you stop learning, you start dying

誰かの学習の一助になりますように

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1