未定义变量:工厂 Laravel 8 Livewire
Undefined variable: factory Laravel 8 Livewire
我是 Laravel/Livewire/Jetstream 的新手,我正在尝试按照此 [教程] (https://lightit.io/blog/laravel-livewire-shopping-cart-demo/) 创建一个购物车项目。我在虚拟框中使用 Laravel 8/ OS Ubuntu。
当我尝试 运行 此命令 php artisan migrate:fresh --seed
时出现异常错误 Undefined variable: factory
错误内容
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/"
}
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
<?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
<?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
<?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);
}
}
你能帮我理解我做错了什么吗?
谢谢
您的 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;
我是 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/"
}
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
<?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
<?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
<?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);
}
}
你能帮我理解我做错了什么吗?
谢谢
您的 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;