Ejemplo 1: laravel tinker de fábrica
php artisan tinker
User::factory()->count(5)->create()
Ejemplo 2: crear un usuario usando laravel de fábrica
1. Create a factory:
php artisan make:factory ItemFactory --model=Item
Import IlluminateDatabaseEloquentFactoriesHasFactory trait to your model:
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Item extends Model
{
use HasFactory;
// ...
}
2. Use it like this:
$item = Item::factory()->make(); // Create a single AppModelsItem instance
// or
$items = Item::factory()->count(3)->make(); // Create three AppModelsItem instances
3. Use create method to persist them to the database:
$item = Item::factory()->create(); // Create a single AppModelsItem instance and persist to the database
// or
$items = Item::factory()->count(3)->create(); // Create three AppModelsItem instances and persist to the database
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)