11. Create a factory:
2 php artisan make:factory ItemFactory --model=Item
3
4Import Illuminate\Database\Eloquent\Factories\HasFactory trait to your model:
5
6use Illuminate\Database\Eloquent\Factories\HasFactory;
7use Illuminate\Database\Eloquent\Model;
8
9class Item extends Model
10{
11 use HasFactory;
12
13 // ...
14}
15
162. Use it like this:
17
18 $item = Item::factory()->make(); // Create a single App\Models\Item instance
19
20 // or
21
22 $items = Item::factory()->count(3)->make(); // Create three App\Models\Item instances
23
243. Use create method to persist them to the database:
25
26 $item = Item::factory()->create(); // Create a single App\Models\Item instance and persist to the database
27
28 // or
29
30 $items = Item::factory()->count(3)->create(); // Create three App\Models\Item instances and persist to the database
31