0

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

enter image description here

enter image description here

enter image description here

enter image description here

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"

enter image description here

and got this error

tried to do the same thing as in the course, and did not worked my code:

my code

course code:

enter image description here

swm
  • 49
  • 1
  • 1
  • 10
  • 1
    which laravel version are you running? – Juan Eizmendi Jul 25 '21 at 02:16
  • 1
    user factory seems to be missing `/* @var $factory \Illuminate\Database\Eloquent\Factory */` at the top. Maybe that is the culprit. – user3532758 Jul 25 '21 at 02:17
  • Laravel Framework 8.50.0 – swm Jul 25 '21 at 03:03
  • it is weird because in the last image (course) the instructor does not include anything as seen there in the screenshot, however i will try whatever suggestion you bring here – swm Jul 25 '21 at 03:04
  • Does [this](https://stackoverflow.com/questions/65720055/undefined-variable-factory-laravel-8-livewire) answer your question? – Doğuş Jul 25 '21 at 03:29
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Martin Zeitler Jul 25 '21 at 03:40
  • 2
    Just revert file `UserFactory.php` to it's original content ... this isn't a valid PHP class. – Martin Zeitler Jul 25 '21 at 03:47
  • 1. `@var..` should be between `/*` and `*/`. 2. If it is Larvel 8, factory definitions will be completely different; UserFactory would have a separate class where definitions would go inside a definitions() function. https://laravel.com/docs/8.x/database-testing#concept-overview (the previous versions might work but I have never tried and no promises) – user3532758 Jul 25 '21 at 04:16
  • https://laravel.com/docs/8.x/upgrade#model-factories use `composer require laravel/legacy-factories` to use legacy style factories – apokryfos Jul 25 '21 at 10:19
  • tried : use composer require laravel/legacy-factories , ran php artisan db:seed still getting the: ErrorException Undefined variable: factory at D:\Repositorio\ApiRestful\database\factories\UserFactory.php:11 7▕ use App\Seller; 8▕ 9▕ /*@var \Illuminate\Database\Eloquent\Factory $factory*/ 10▕ ➜ 11▕ $factory->define(User::class, function (Faker\Generator $faker) – swm Jul 25 '21 at 15:50
  • Ok saw this video, https://www.youtube.com/watch?v=Qrff3Qiy2yk , so basically do i have to create a factoryClassFile.php for every class? for example, i have this code: https://jsfiddle.net/p2h6otcr/ do i have to create a factoryfile for every model CLASS? for example, USER.php and CATEGORY.php ? thank in foward – swm Jul 25 '21 at 16:33

0 Answers0