Unable to use Laravel Factory in Tinker
Asked Answered
D

5

17

I am unable Model Factory in Laravel Tinker.

//ItemFactory.php

class ItemFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Item::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'name' => $this->faker->name,
            'slug' => $this->faker->slug(5, true),
            'code' => $this->faker->words(5, true),
            'description' => $this->faker->sentence,
            'price' => $this->faker->randomNumber(1000, 10000),
            'size' => $this->faker->randomElement(['Small', 'Medium', 'Large',]),
        ];
    }
}

Inside Tinker

>>> factory(App\Item::class)->create();

It throws me an error:

PHP Fatal error: Call to undefined function factory() in Psy Shell code on line 1

Dumont answered 10/9, 2020 at 7:4 Comment(0)
D
17

After going through the documentation of Model Factory, there were major changes in Laravel 8 version.

For using Model Factory anywhere inside Laravel 8:

  1. Inside Model, we need to import the Illuminate\Database\Eloquent\Factories\HasFactory trait

  2. New command to implement the factory

App\Item::factory()->create();
Dumont answered 10/9, 2020 at 7:9 Comment(1)
Illuminate\Database\Eloquent\Factories\HasFactory In laravel 8 it's by default imported when make model using commandWriggler
W
18

In laravel 8 the default route namespace was removed.

Try to change command

factory(App\Item::class)->create();

To

\App\Models\Item::factory()->create(); 
\App\Models\Item::factory(10)->create(); \\If you want to create specify number of record then
Wriggler answered 31/1, 2021 at 16:33 Comment(3)
This worked great in Laravel 9 as well.Alard
Thanks @Alard for share such a usefull information.I will help lot's of developerWriggler
Thanks it worked for me using laravel 9Intrust
D
17

After going through the documentation of Model Factory, there were major changes in Laravel 8 version.

For using Model Factory anywhere inside Laravel 8:

  1. Inside Model, we need to import the Illuminate\Database\Eloquent\Factories\HasFactory trait

  2. New command to implement the factory

App\Item::factory()->create();
Dumont answered 10/9, 2020 at 7:9 Comment(1)
Illuminate\Database\Eloquent\Factories\HasFactory In laravel 8 it's by default imported when make model using commandWriggler
D
17

In Laravel 8.x release notes:

Eloquent model factories have been entirely re-written as class based factories and improved to have first-class relationship support.

Global factory() function is removed as of Laravel 8. Instead, you should now use model factory classes.

  1. Create a factory:
php artisan make:factory ItemFactory --model=Item
  1. Make sure that Illuminate\Database\Eloquent\Factories\HasFactory trait is imported in your model:
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
    use HasFactory;

    // ...
}
  1. Use it like this:
$item = Item::factory()->make(); // Create a single App\Models\Item instance

// or

$items = Item::factory()->count(3)->make(); // Create three App\Models\Item instances

Use create method to persist them to the database:

$item = Item::factory()->create(); // Create a single App\Models\Item instance and persist to the database

// or

$items = Item::factory()->count(3)->create(); // Create three App\Models\Item instances and persist to the database

Being said that, if you still want to provide support for the previous generation of model factories within Laravel 8.x, you can use laravel/legacy-factories package.

Dyann answered 12/9, 2020 at 11:30 Comment(0)
M
1

In Laravel 8 I couldn't get the factory method to work just calling it straight out but I was able to get it to work by calling it like a static method on the Item class:

Terminal:

Item::factory()->create();

Multiplicand answered 19/6, 2022 at 17:25 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Sladen
B
0

In laravel 10

  1. Make sure in your model Illuminate\Database\Eloquent\Factories\HasFactory is imported your model should look like this
  use Illuminate\Database\Eloquent\Model;
  
  class Item extends Model
  {
      use HasFactory;
  
      // ...
  }

Create a factory

php artisan make:factory ItemFactory --model=Item

Define factory In open tinker

php artisan tinker

Then

use App\Models\model-name(ie Item) Item::factory()->create()

To insert 4 records perhaps you can specify factory parameter

item::factory(4)->create()

Brezin answered 28/9, 2023 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.