1$faker->numerify('###-###-####');
2// "766-620-7004"
3
4$faker->numerify('##########');
5// "3579786681"
1random_int(0, 12300)
2Str::random(20)
3 $faker->word
4 $faker->sentence
5 $faker->paragraph
6 $faker->randomFloat(2, 0, 10000)
7 $faker->image('public/storage/images',640,480, null, false),
8 $faker->text(200)
9 $faker->numberBetween(10, 500)
10 $faker->unique()->numberBetween(100, 500)
11 $faker->userName,
12$faker -> sentence(4,true),
13<img src="/storage/images/{{$product->image}}">
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
1factory(App\User::class, 30)->create()->each(function($user) {
2
3 $entity = factory(App\Entity::class)->make();
4
5 $address = factory(App\Address::class)->create([
6 'entity_id' => $entity
7 ]);
8
9 $user->entities()->save($entity);
10});