URL Shortener 1.0
A Laravel-based URL shortener application
Chargement...
Recherche...
Aucune correspondance
ShortUrlSeeder.php
Aller à la documentation de ce fichier.
1<?php
2
3namespace Database\Seeders;
4
7use Illuminate\Database\Seeder;
8
14class ShortUrlSeeder extends Seeder
15{
19 public function run(): void
20 {
21 // Create short URLs for each user
22 $users = User::all();
23
24 foreach ($users as $user) {
25 // Create 5-20 URLs per user with different scenarios
26 ShortUrl::factory()
27 ->count(rand(3, 8))
28 ->create(['user_id' => $user->id]);
29
30 // Some URLs never used
31 ShortUrl::factory()
32 ->neverUsed()
33 ->count(rand(1, 3))
34 ->create(['user_id' => $user->id]);
35
36 // Some URLs recently used
37 ShortUrl::factory()
38 ->recentlyUsed()
39 ->count(rand(1, 3))
40 ->create(['user_id' => $user->id]);
41
42 // Some URLs with high traffic
43 ShortUrl::factory()
44 ->highTraffic()
45 ->count(rand(0, 2))
46 ->create(['user_id' => $user->id]);
47 }
48
49 // Create specific URLs for testing
50 $testUser = User::where('email', 'test@example.com')->first();
51
52 if ($testUser) {
53 ShortUrl::factory()->create([
54 'user_id' => $testUser->id,
55 'code' => '123456',
56 'original_url' => 'https://www.google.com',
57 'clicks' => 150,
58 'last_used_at' => now()->subDays(2),
59 ]);
60
61 ShortUrl::factory()->create([
62 'user_id' => $testUser->id,
63 'code' => '789012',
64 'original_url' => 'https://www.github.com',
65 'clicks' => 0,
66 'last_used_at' => null,
67 ]);
68
69 ShortUrl::factory()->create([
70 'user_id' => $testUser->id,
71 'code' => '345678',
72 'original_url' => 'https://www.laravel.com',
73 'clicks' => 89,
74 'last_used_at' => now()->subMonths(3),
75 ]);
76 }
77 }
78}
This class is responsible for seeding the database with short URLs.
run()
Run the database seeds.