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
error content
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 file
"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);
}
}
Can you please help me to understand what I did wrong?
Thanks