Skip to content

Commit afa8d8e

Browse files
committed
Remove external dependency to seed blog images.
1 parent b407563 commit afa8d8e

File tree

4 files changed

+70
-83
lines changed

4 files changed

+70
-83
lines changed

database/seeders/DatabaseSeeder.php

+67-28
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Database\Seeders;
44

5+
use Illuminate\Database\Eloquent\Factories\Sequence;
56
use Illuminate\Database\Seeder;
67
use Illuminate\Support\Facades\Schema;
7-
use Illuminate\Support\Facades\Storage;
88
use Modules\Acl\Database\Seeders\AclModelHasRolesSeeder;
99
use Modules\Acl\Database\Seeders\AclPermissionSeeder;
1010
use Modules\Acl\Database\Seeders\AclRoleHasPermissionsSeeder;
@@ -15,19 +15,73 @@
1515
use Modules\User\Database\Seeders\UserSeeder;
1616

1717
use function Laravel\Prompts\info;
18-
use function Laravel\Prompts\progress;
1918

2019
class DatabaseSeeder extends Seeder
2120
{
21+
private $blogCategoriesImages = [
22+
'57b76f29-dd7d-4018-b172-a06e6ef4a4cf.jpg',
23+
'f0d5596c-d23a-4db7-97a5-9a298b924994.jpg',
24+
'44a928b1-5148-4940-af48-2792f1151272.jpg',
25+
'5febe62d-d9bd-4054-b5a1-83b796ca9d14.jpg',
26+
'9ab99b32-1f04-495e-b5a2-5fa789dedd18.jpg',
27+
'24521518-b796-445f-8e9a-59233d8d78fa.jpg',
28+
'0d2dc407-6b6f-4022-9565-1ecec79daf52.jpg',
29+
'153cfa68-8ca8-4712-a1c1-1bd969796ef9.jpg',
30+
'3d7da554-1a6c-44f6-9f9f-2a6947446a65.jpg',
31+
'cd2dc303-24c8-41b3-b09e-83205a647d8f.jpg',
32+
'edb880ab-4e13-4594-b6d1-fa4125a00100.jpg',
33+
'034cb604-d3d0-448b-92f1-6a255fb4653d.jpg',
34+
];
35+
36+
private $blogAuthorsImages = [
37+
'a98ea6d4-9f8b-4897-9c63-bdafc1acd602.jpg',
38+
'77aa4a7a-de06-4ae6-8a1d-42322f532c53.jpg',
39+
'da4bc233-b12b-4548-904c-b3cf70711607.jpg',
40+
'b51306e7-f8f8-49b3-9083-e3fffccad1b4.jpg',
41+
'8c183760-1de3-4f6a-bef6-7241e7ebbb2f.jpg',
42+
'dc44c3b1-6de3-4c47-80bb-e28de18f314b.jpg',
43+
'850e4903-66fe-4feb-a218-64058bb84a6a.jpg',
44+
'cf45d6f9-0ff0-41ae-966b-a7fa50f08e33.jpg',
45+
'fe15a944-36e4-4c0b-a634-f9e6c00df101.jpg',
46+
'15dcfc3d-f8f4-470e-9477-72322e399731.jpg',
47+
'735aa22c-6aa0-4ced-9552-5494f0da4e5e.jpg',
48+
'bac12f21-8eaa-437e-bf0c-2ae84ca2b02a.jpg',
49+
];
50+
51+
private $blogPostsImages = [
52+
'faa02464-6d3e-49b9-a45c-de303c91f3d5.jpg',
53+
'49814997-b619-4281-842f-8729f2ec151c.jpg',
54+
'e9d82a31-b7a2-46d8-9da4-48f51f4207aa.jpg',
55+
'e42eefc0-7f34-4645-ba21-d9c931daf08f.jpg',
56+
'f5e456c2-de4b-48c2-9554-54b8b8e7bfbc.jpg',
57+
'2a7f2576-82a1-4f28-9f38-5a9cc216ba16.jpg',
58+
'94ab2a3f-2300-4754-b458-2cc36cceac05.jpg',
59+
'e6ebdade-56ec-4d59-9da2-cf479b7d8c46.jpg',
60+
'f6d0c648-5d59-4b96-9c44-ebdc0ba3ee38.jpg',
61+
'3fe0425a-96a4-49b5-bcde-adf75caa626d.jpg',
62+
'500862ad-b078-4306-9348-39edf65f6d05.jpg',
63+
'2348a092-55d0-4b6d-b43d-3b34280dbed9.jpg',
64+
'57135dbe-7d31-4380-85cd-b78179ca2400.jpg',
65+
'75fca485-2ea3-47d0-b609-9c8bd58d3438.jpg',
66+
'4c798d2c-c4a6-4293-b052-98a048914215.jpg',
67+
'b8cac8c5-c3d2-4a65-8ebb-1b74059fe309.jpg',
68+
'952536c8-e74c-4089-bce4-3cc3b474a2d2.jpg',
69+
'5f4ecece-6275-41b9-bd64-0e6f904a5df7.jpg',
70+
'463c0606-1382-49b3-8ba5-3a3b91c4ff09.jpg',
71+
'd27f5f87-f8dd-47df-8b2e-5086a5ba66c1.jpg',
72+
'5a888786-090c-40f1-85e0-f86895311eeb.jpg',
73+
'68255060-d85e-4081-a025-931789e6aa1d.jpg',
74+
'4e8d6a91-e0ef-48aa-86cd-8d18699407c3.jpg',
75+
'548857a2-c1b5-490e-b3a2-20a28ba98e0f.jpg',
76+
];
77+
2278
/**
2379
* Seed the application's database.
2480
*
2581
* @return void
2682
*/
2783
public function run()
2884
{
29-
Storage::deleteDirectory('public/blog');
30-
3185
Schema::disableForeignKeyConstraints();
3286

3387
info('Creating users...');
@@ -51,38 +105,23 @@ public function run()
51105
info('ACL roles assigned to users.');
52106

53107
info('Creating blog categories...');
54-
$this->seedWithProgress(
55-
label: 'Categories creation status',
56-
steps: 12,
57-
callback: fn () => Category::factory()->create(),
58-
);
108+
Category::factory()->count(12)
109+
->sequence(fn (Sequence $sequence) => ['image' => $this->blogCategoriesImages[$sequence->index]])
110+
->create();
59111
info('Blog categories created.');
60112

61113
info('Creating blog authors...');
62-
$this->seedWithProgress(
63-
label: 'Authors creation status',
64-
steps: 12,
65-
callback: fn () => Author::factory()->create(),
66-
);
114+
Author::factory()->count(12)
115+
->sequence(fn (Sequence $sequence) => ['image' => $this->blogAuthorsImages[$sequence->index]])
116+
->create();
67117
info('Blog authors created.');
68118

69119
info('Creating blog posts...');
70-
$this->seedWithProgress(
71-
label: 'Posts creation status',
72-
steps: 24,
73-
callback: fn () => Post::factory()->create(),
74-
);
120+
Post::factory()->count(24)
121+
->sequence(fn (Sequence $sequence) => ['image' => $this->blogPostsImages[$sequence->index]])
122+
->create();
75123
info('Blog posts created.');
76124

77125
Schema::enableForeignKeyConstraints();
78126
}
79-
80-
private function seedWithProgress(string $label, int $steps, callable $callback): void
81-
{
82-
progress(
83-
label: $label,
84-
steps: $steps,
85-
callback: $callback,
86-
);
87-
}
88127
}

modules/Blog/Database/Factories/BlogAuthorFactory.php

+1-19
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
namespace Modules\Blog\Database\Factories;
44

55
use Illuminate\Database\Eloquent\Factories\Factory;
6-
use Illuminate\Support\Facades\Storage;
7-
use Illuminate\Support\Str;
86
use Modules\Blog\Models\Author;
9-
use Throwable;
107

118
class BlogAuthorFactory extends Factory
129
{
@@ -18,26 +15,11 @@ public function definition(): array
1815
'name' => $this->faker->name(),
1916
'email' => $this->faker->unique()->safeEmail(),
2017
'bio' => $this->faker->realTextBetween(),
21-
'image' => $this->createImage(),
18+
'image' => $this->faker->imageUrl(),
2219
'github_handle' => $this->faker->userName(),
2320
'twitter_handle' => $this->faker->userName(),
2421
'created_at' => $this->faker->dateTimeBetween('-1 year', '-3 month'),
2522
'updated_at' => $this->faker->dateTimeBetween('-2 month', 'now'),
2623
];
2724
}
28-
29-
private function createImage(): string
30-
{
31-
try {
32-
$image = file_get_contents('https://source.unsplash.com/random/240x240?adult');
33-
} catch (Throwable $exception) {
34-
return 'Error fetching image: '.$exception->getMessage();
35-
}
36-
37-
$fileName = Str::uuid().'.jpg';
38-
39-
Storage::disk('public')->put("blog/$fileName", $image);
40-
41-
return $fileName;
42-
}
4325
}

modules/Blog/Database/Factories/BlogCategoryFactory.php

+1-18
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
namespace Modules\Blog\Database\Factories;
44

55
use Illuminate\Database\Eloquent\Factories\Factory;
6-
use Illuminate\Support\Facades\Storage;
76
use Illuminate\Support\Str;
87
use Modules\Blog\Models\Category;
9-
use Throwable;
108

119
class BlogCategoryFactory extends Factory
1210
{
@@ -19,7 +17,7 @@ public function definition(): array
1917
return [
2018
'name' => $name,
2119
'description' => $this->faker->realText(),
22-
'image' => $this->createImage(),
20+
'image' => $this->faker->imageUrl(),
2321
'is_visible' => $this->faker->boolean(),
2422
'slug' => Str::slug($name),
2523
'meta_tag_title' => Str::limit($name, 60, ''),
@@ -28,19 +26,4 @@ public function definition(): array
2826
'updated_at' => $this->faker->dateTimeBetween('-5 month', 'now'),
2927
];
3028
}
31-
32-
private function createImage(): string
33-
{
34-
try {
35-
$image = file_get_contents('https://source.unsplash.com/random/320x75?nature');
36-
} catch (Throwable $exception) {
37-
return 'Error fetching image: '.$exception->getMessage();
38-
}
39-
40-
$fileName = Str::uuid().'.jpg';
41-
42-
Storage::disk('public')->put("blog/$fileName", $image);
43-
44-
return $fileName;
45-
}
4629
}

modules/Blog/Database/Factories/BlogPostFactory.php

+1-18
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
namespace Modules\Blog\Database\Factories;
44

55
use Illuminate\Database\Eloquent\Factories\Factory;
6-
use Illuminate\Support\Facades\Storage;
76
use Illuminate\Support\Str;
87
use Modules\Blog\Models\Post;
9-
use Throwable;
108

119
class BlogPostFactory extends Factory
1210
{
@@ -20,27 +18,12 @@ public function definition(): array
2018
'title' => $title,
2119
'slug' => Str::slug($title),
2220
'content' => $this->faker->realText(),
23-
'image' => $this->createImage(),
21+
'image' => $this->faker->imageUrl(),
2422
'meta_tag_title' => Str::limit($title, 60, ''),
2523
'meta_tag_description' => Str::limit($title, 160, ''),
2624
'published_at' => $this->faker->dateTimeBetween('-6 month', '+3 month'),
2725
'created_at' => $this->faker->dateTimeBetween('-1 year', '-6 month'),
2826
'updated_at' => $this->faker->dateTimeBetween('-5 month', 'now'),
2927
];
3028
}
31-
32-
private function createImage(): string
33-
{
34-
try {
35-
$image = file_get_contents('https://source.unsplash.com/random/240x240?nature');
36-
} catch (Throwable $exception) {
37-
return 'Error fetching image: '.$exception->getMessage();
38-
}
39-
40-
$fileName = Str::uuid().'.jpg';
41-
42-
Storage::disk('public')->put("blog/$fileName", $image);
43-
44-
return $fileName;
45-
}
4629
}

0 commit comments

Comments
 (0)