【问题标题】:Undefined variable: factory Laravel 8 Livewire [duplicate]未定义的变量:工厂 Laravel 8 Livewire [重复]
【发布时间】:2021-04-19 12:51:28
【问题描述】:

我是 Laravel/Livewire/Jetstream 的新手,我正在尝试按照这个 [教程] (https://lightit.io/blog/laravel-livewire-shopping-cart-demo/) 创建一个购物车项目。我在虚拟框中使用 Laravel 8/ OS Ubuntu。 当我尝试运行此命令php artisan migrate:fresh --seed时,出现异常错误未定义变量:工厂@

错误内容

Seeding: Database\Seeders\ProductSeeder

   ErrorException 

  Undefined variable: factory

  at database/factories/ProductFactory.php:7
      3▕ /** @var \Illuminate\Database\Eloquent\Factory $factory */
      4▕ use App\Models\Product;
      5▕ use Faker\Generator as Faker;
      6▕ 
  ➜   7▕ $factory->define(Product::class, function (Faker $faker) {
      8▕     return [
      9▕         'name' => $faker->word,
     10▕         'description' => $faker->text(180),
     11▕         'price' => $faker->numberBetween(50, 100)

  1   database/factories/ProductFactory.php:7
      Illuminate\Foundation\Bootstrap\HandleExceptions::handleError()

      +2 vendor frames 
  4   [internal]:0
      Composer\Autoload\ClassLoader::loadClass()

composer.json 文件

 "autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    }

数据库/迁移/xxxx_xx_xx_xxxxxx_create_products_table.php

<?php

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

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

            $table->string('name');
            $table->string('description');
            $table->float('price');
            
            $table->timestamps();
        });
    }

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

数据库/工厂/ProductFactory.php

    <?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Models\Product;
use Faker\Generator as Faker;

$factory->define(Product::class, function (Faker $faker) {
    return [
        'name' => $faker->word,
        'description' => $faker->text(180),
        'price' => $faker->numberBetween(50, 100)
    ];
});

数据库/seeders/ProductSeeder.php

    <?php
namespace Database\Seeders;

use App\Models\Product;
use Illuminate\Database\Seeder;

class ProductSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        \App\Models\Product::factory()->count(50)->create();
    }
}

数据库/seeders/DatabaseSeeder.php

    <?php

use Database\Seeders\ProductSeeder;
use Illuminate\Database\Seeder;


class DatabaseSeeder extends Seeder
'{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(ProductSeeder::class);
    }
}

你能帮我理解我做错了什么吗?

谢谢

【问题讨论】:

    标签: php database virtualbox shopping-cart laravel-livewire


    【解决方案1】:

    您的 ProductFactory::class 似乎遵循 Laravel 7 约定。 Laravel 8 模型工厂功能已完全重写以支持类,并且与 Laravel 7.x 样式工厂不兼容。

    您可以这样做:

    <?php
    
    namespace Database\Factories;
    
    use App\Models\Product;
    use Illuminate\Database\Eloquent\Factories\Factory;
    use Illuminate\Support\Str;
    
    class ProductFactory extends Factory
    {
        /**
         * The name of the factory's corresponding model.
         *
         * @var string
         */
        protected $model = Product::class;
    
        /**
         * Define the model's default state.
         *
         * @return array
         */
        public function definition()
        {
            return [
                'name' => $this->faker->name,
                'description' => $this->faker->realText(180),
                'price' => $this->faker->numberBetween(50, 100)
            ];
        }
    }
    

    此外,您还需要更新您的Product Model

    use Illuminate\Database\Eloquent\Factories\HasFactory;
    
    class Product extends Model
    {
        use HasFactory;
    

    【讨论】:

    • 谢谢,它有效。我的产品模型已经更新。
    猜你喜欢
    • 2021-03-26
    • 2021-09-18
    • 2021-05-11
    • 2023-01-14
    • 1970-01-01
    • 2021-10-31
    • 2021-05-10
    • 1970-01-01
    • 2015-09-19
    相关资源
    最近更新 更多