i am getting this error: i am trying to do a php artisan db:seed but i cannot since $factory is a undefined variable, i am doing exactly as i see in the course i am on, but since i am new on this a cannot figure how to make it work i am posting it here, here is the error and code and screenshots: thank in foward
vagrant@apirestful:~/apirestful$ php artisan db:seed
ErrorException
Undefined variable $factory
at database/factories/UserFactory.php:9
5▕
6▕ use App\Models\Transaction;
7▕ use App\Seller;
8▕
➜ 9▕ $factory->define(User::class, function (Faker\Generator $faker) {
10▕ static $password;
11▕
12▕ return [
13▕ 'name' => $faker->name,
1 database/factories/UserFactory.php:9
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError()
+4 vendor frames
6 database/seeders/DatabaseSeeder.php:37
App\Models\User::factory()
userfactory.php
<?php
use App\Models\User;
use App\Models\Product;
use App\Models\Transaction;
use App\Seller;
$factory->define(User::class, function (Faker\Generator $faker) {
static $password;
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
'verified' => $verificado = $faker->randomElement([User::USUARIO_VERIFICADO, User::USUARIO_NO_VERIFICADO]),
'verification_token' => $verificado == User::USUARIO_VERIFICADO ? null : User::generarVerficiationToken(),
'admin' => $faker->randomElement([User::USUARIO_ADMINISTRADOR, User::USUARIO_REGULAR]),
];
});
$factory->define(Category::class, function (Faker\Generator $faker) {
return [
'name' => $faker->word,
'description' => $faker->paragraph(1),
];
});
$factory->define(Product::class, function (Faker\Generator $faker) {
return [
'name' => $faker->word,
'description' => $faker->paragraph(1),
'quantity' => $faker->numberBetween(1, 10),
'status' => $faker->randomElement([Product::PRODUCTO_DISPONIBLE, Product::PRODUCTO_NO_DISPONIBLE]),
'image' => $faker->randomElement(['1.jpg', '2.jpg', '3.jpg']),
'seller_id' => User::all()->random()->id,
];
});
$factory->define(Transaction::class, function (Faker\Generator $faker) {
$vendedor = Seller::has('products')->get()->random();
$comprador = User::all()->except($vendedor->id)->random();
return [
'name' => $faker->word,
'quantity' => $faker->numberBetween(1, 3),
'buyer_id' => $comprador->id,
'product_id' => $vendedor->products->random()->id,
];
});
added the solution of
"user factory seems to be missing /* @var $factory \Illuminate\Database\Eloquent\Factory */ at the top. Maybe that is the culprit. – user3532758"
and got this error
tried to do the same thing as in the course, and did not worked my code:
course code: