无法在 Tinker 中使用 Laravel 工厂
Unable to use Laravel Factory in Tinker
我无法在 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',]),
];
}
}
修补匠内部
>>> factory(App\Item::class)->create();
它抛出一个错误:
PHP Fatal error: Call to undefined function factory() in Psy Shell
code on line 1
经过 documentation of Model Factory 之后,Laravel 8 版本 发生了重大变化。
在 Laravel 8 中的任何地方使用模型工厂:
在模型内部,我们需要导入 Illuminate\Database\Eloquent\Factories\HasFactory
trait
实施工厂的新命令
App\Item::factory()->create();
在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.
全局 factory()
函数已从 Laravel 8 开始删除。相反,您现在应该使用 model factory classes.
- 创建工厂:
php artisan make:factory ItemFactory --model=Item
- 确保
Illuminate\Database\Eloquent\Factories\HasFactory
特征已导入您的模型:
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
话虽如此,如果你还想在Laravel8.x内为上一代模型工厂提供支持,你可以使用laravel/legacy-factories包。
在 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 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',]),
];
}
}
修补匠内部
>>> factory(App\Item::class)->create();
它抛出一个错误:
PHP Fatal error: Call to undefined function factory() in Psy Shell code on line 1
经过 documentation of Model Factory 之后,Laravel 8 版本 发生了重大变化。
在 Laravel 8 中的任何地方使用模型工厂:
在模型内部,我们需要导入
Illuminate\Database\Eloquent\Factories\HasFactory
trait实施工厂的新命令
App\Item::factory()->create();
在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.
全局 factory()
函数已从 Laravel 8 开始删除。相反,您现在应该使用 model factory classes.
- 创建工厂:
php artisan make:factory ItemFactory --model=Item
- 确保
Illuminate\Database\Eloquent\Factories\HasFactory
特征已导入您的模型:
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
话虽如此,如果你还想在Laravel8.x内为上一代模型工厂提供支持,你可以使用laravel/legacy-factories包。
在 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