首页 > 解决方案 > 未定义的方法数据库\工厂\ 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' => '$2y$10$.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();
    
        
    }
}

这是我做fakers数据的主文件My 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)

        ];
    }
}

我不知道用表结构显示迁移是否也有用

<?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');
    }
}

标签: phplaravelfactorylaravel-8

解决方案


请验证您use HasFactory在 Post 模型中是否具有特征,并且当您使用 laravel 8 时,请将url_imagePostfactory.php 中的替换为以下内容:

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

推荐阅读