6

我有上述错误,这是复制日志

php artisan db:seed

   BadMethodCallException

  Call to undefined method App\Models\Category::factory()

  at vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:50
     46▕      * @throws \BadMethodCallException
     47▕      */
     48▕     protected static function throwBadMethodCallException($method)
     49▕     {
  ➜  50▕         throw new BadMethodCallException(sprintf(
     51▕             'Call to undefined method %s::%s()', static::class, $method
     52▕         ));
     53▕     }
     54▕ }

  • Bad Method Call: Did you mean App\Models\Category::toArray() ?

      +3 vendor frames
  4   database/seeders/DatabaseSeeder.php:38
      Illuminate\Database\Eloquent\Model::__callStatic()

      +22 vendor frames
  27  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

这是 databaseseeder.php 类,因为存在错误:

<?php

namespace Database\Seeders;

use App\Models\Category;
use App\Models\Product;
use App\Models\Transaction;
use App\Models\User;
use Illuminate\Database\Seeder;

use Illuminate\Support\Facades\DB;

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

        DB::statement('SET FOREIGN_KEY_CHECKS=0');

        User::truncate();
        Category::truncate();
        Product::truncate();
        Transaction::truncate();
        DB::table('category_product')->truncate();

        $cantidadUsuarios = 200;
        $cantidadCategories = 30;
        $cantidadProductos = 1000;
        $cantidadTransacciones = 1000;

        \App\Models\User::factory()->count($cantidadUsuarios)->create();
        \App\Models\Category::factory()->count($cantidadUsuarios)->create();

        \App\Models\Product::factory()->count($cantidadTransacciones)->create()->each                                                                                           (
            function ($producto) {
                $categorias = Category::all()->random(mt_rand(1, 5))->pluck('id');
                $producto->categories()->attach($categorias);
            }
        );

        \App\Models\Transaction::factory()->count($cantidadTransacciones)->create();
    }
}

有错误行:

\App\Models\Category::factory()->count($cantidadUsuarios)->create();

这里我们得到了类别类:

<?php

namespace App\Models;

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

class Category extends Model
{
    public $table = "categories";

    protected $fillable = [
        'name',
        'description',
    ];
}

在这里,我们得到了类别工厂:

<?php

namespace Database\Factories;

use App\Models\Category;
use Illuminate\Database\Eloquent\Factories\Factory;

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

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            //
             'name' => $this->faker->word,
            'description' => $this->faker->paragraph(1),
        ];
    }
}

这是类别迁移:

<?php

use App\Models\Product;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('description', 1000);
            $table->integer('quantity')->unsigned();
            $table->string('status')->default(Product::PRODUCTO_NO_DISPONIBLE);
            $table->string('image');
            $table->integer('seller_id')->unsigned();
            $table->timestamps();

            $table->foreign('seller_id')->references('id')->on('users');
        });
    }

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

我发布了所有你需要的东西,因为我是新手,我找不到问题。

4

3 回答 3

15

您需要使用模型的工厂特征才能使该factory()方法可用。

class Category extends Model
{
   use HasFactory;

   ...
}
于 2021-08-01T00:51:24.957 回答
2

遵循以下代码:

<?php

namespace App\Models;

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

class Category extends Model
{
    use HasFactory;
}
于 2021-10-02T07:42:07.307 回答
-1

如果您正在使用 tinker,则在添加 Use HasFactory 类之后,只需结束当前会话并开始另一个会话。

  1. Ctrl+C
  2. PHP工匠修补匠
于 2022-02-01T09:48:37.800 回答