未定义变量:工厂 Laravel 8 Livewire [英] Undefined variable: factory Laravel 8 Livewire

查看:40
本文介绍了未定义变量:工厂 Laravel 8 Livewire的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Laravel/Livewire/Jetstream 的新手,我正在尝试按照本 [教程] (https://lightit.io/blog/laravel-livewire-shopping-cart-demo/).我在虚拟框中使用 Laravel 8/OS Ubuntu.当我尝试运行此命令时,出现异常错误 Undefined variable: factory php artisan migrate:fresh --seed

I am new to Laravel/Livewire/Jetstream and I am trying to create a shopping cart project following this [tutorial] (https://lightit.io/blog/laravel-livewire-shopping-cart-demo/). I am using Laravel 8/ OS Ubuntu in virtual box. I get an exception error Undefined variable: factory when I tried to run this command 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 文件

composer.json file

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

database/migrations/xxxx_xx_xx_xxxxxx_create_products_table.php

database/migrations/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');
    }
}

database/factories/ProductFactory.php

database/factories/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)
    ];
});

database/seeders/ProductSeeder.php

database/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();
    }
}

database/seeders/DatabaseSeeder.php

database/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);
    }
}

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

Can you please help me to understand what I did wrong?

谢谢

推荐答案

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

Your ProductFactory::class seems to following Laravel 7 conventions. Laravel 8 model factories feature has been totally rewritten to support classes and is not compatible with Laravel 7.x style factories.

您可以这样做:

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

此外,您还需要更新您的产品模型:

Also, you will need to update your Product Model as well:

use Illuminate\Database\Eloquent\Factories\HasFactory;

class Product extends Model
{
    use HasFactory;

这篇关于未定义变量:工厂 Laravel 8 Livewire的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆