无法在Tinker中使用Laravel工厂

17
我无法在 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',]),
        ];
    }
}

探寻Tinker的内部

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

它向我抛出一个错误:

PHP致命错误:在Psy Shell代码的第1行调用未定义的函数factory()

5个回答

17

在 Laravel 8 中,默认的路由命名空间被移除。

尝试更改命令。

factory(App\Item::class)->create();
\App\Models\Item::factory()->create(); 
\App\Models\Item::factory(10)->create(); \\If you want to create specify number of record then

这在 Laravel 9 中也非常有效。 - Adam
感谢@Adam分享如此有用的信息。这将帮助许多开发人员。 - Bhargav Variya
谢谢,使用 Laravel 9 对我很有帮助。 - Nevil Saul Blemuss

17

阅读模型工厂文档后,发现在Laravel 8版本中有很多重大变化。

要在Laravel 8中任何地方使用模型工厂:

  1. 在模型中,我们需要导入Illuminate\Database\Eloquent\Factories\HasFactory trait

  2. 实现工厂的新命令

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

2
在 Laravel 8 中,使用命令创建模型时,默认会导入 Illuminate\Database\Eloquent\Factories\HasFactory。 - Bhargav Variya

17
在 Laravel 8.x 发布说明 中:
Eloquent 模型工厂已完全重写为基于类的工厂,并改进为具有一流关系支持。
全局 factory() 函数已在 Laravel 8 中删除。现在,您应该使用模型工厂类
创建工厂:
php artisan make:factory ItemFactory --model=Item

确保在你的模型中导入了Illuminate\Database\Eloquent\Factories\HasFactory trait:
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
    use HasFactory;

    // ...
}

请这样使用它:
$item = Item::factory()->make(); // Create a single App\Models\Item instance

// or

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

使用create方法将它们持久化到数据库:

$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

话虽如此,如果您仍希望为Laravel 8.x中的上一代模型工厂提供支持,可以使用laravel/legacy-factories包。


1
在 Laravel 8 中,我无法直接调用工厂方法使其正常工作,但我可以通过在 Item 类上像静态方法一样调用它来使其正常工作:
终端命令:
Item::factory()->create();

你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心找到有关如何编写良好答案的更多信息。 - Community

0
在 Laravel 10 中,
1. 确保在你的模型中导入了 `Illuminate\Database\Eloquent\Factories\HasFactory`,你的模型应该如下所示:
  use Illuminate\Database\Eloquent\Model;
  
  class Item extends Model
  {
      use HasFactory;
  
      // ...
  }

创建一个工厂
``` php artisan make:factory ItemFactory --model=Item ```
定义工厂 在打开的 tinker 中
``` php artisan tinker ```
然后
``` use App\Models\model-name(ie Item) Item::factory()->create() ```
如果要插入4条记录,可以指定工厂参数
``` item::factory(4)->create() ```

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接