未定义的方法 Database\Factories\ Laravel 8

undefined method Database\Factories\ Laravel 8

我对工厂和种子的怀疑。 我尝试使用种子进行迁移,但出现以下问题。 Call to undefined method Database\Factories\PostFactory::faker()

我不太清楚在哪里进行调用,如果在单独的文件中,例如 PostSeeds 或者我在这里是如何进行的。

这是主文件DatabaseSeeder.php

<?php

namespace Database\Seeders;

use App\Models\Category;
use App\Models\Post;
use App\Models\User;
use Illuminate\Support\Facades\Storage;


use Illuminate\Database\Seeder;


class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // \App\Models\User::factory(10)->create();

        Storage::makeDirectory('posts');

        User::create([
            'name' => 'Fernando',
            'email' => 'cainuriel@gmail.com',
            'password' => 'y$.CjCfp.mzo.AUMPZj0n7mOK3zU6QmrabKXwaYcHNFtK1qWHRqf4Xe',
        ]);

        Category::create([
            'name' => 'Plagas',
            'slug' => 'plagas',

        ]);

        Category::create([
            'name' => 'Enfermedades plantas',
            'slug' => 'enfermedades-plantas',

        ]);

        Category::create([
            'name' => 'Cultivo ecológico',
            'slug' => 'cultivo-ecologico',

        ]);

        Category::create([
            'name' => 'Consciencia ecológica',
            'slug' => 'consciencia-ecologica',

        ]);

        Post::factory(10)->create();
    
        
    }
}

这是我的主文件Postfactory.php,我在其中制作伪造数据。

<?php

namespace Database\Factories;

use App\Models\Category;
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class PostFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Post::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        $name =  $this->faker->unique()->sentence();

        return [
            
            'name' => $name,
            'slug' => Str::slug($name),
            'extract' => $this->faker->text(250),
            'body' => $this->faker->text(2000),
            'status' => 2,
            'category_id' => Category::all()->random()->id,
            'user_id' => 1,
            'url_image' => 'posts'.$this->faker('public/storage/posts', 650, 490, null, false)

        ];
    }
}

不知道用table结构展示迁移是否也有用

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();

            $table->string('name');
            $table->string('slug');

            $table->text('extract');
            $table->longtext('body');

            $table->enum('status', [1, 2])->default(1); // 1. Borrador. 2. Publicar.

            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('category_id');

            $table->string('url_image')->default('default.png');

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('category_id')->references('id')->on('categories');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

请确认您在 Post 模型中具有 use HasFactory 特征,并且在您使用 laravel 8 时,将 Postfactory.php 中的 url_image 替换为以下:

'url_image'  => $this->faker->image('public/storage/posts', 650, 490, null, false)