0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Fakerで画像を扱う方法

Posted at

1:ファクトリの生成

% php artisan make:factory ⚪︎⚪︎Factory

2:ダミーデータの日本語化

/Applications/MAMP/htdocs/Laravel/EXP-alert/config/app.php
'fallback_locale' => 'ja_JP',

3:mmo/faker-images をインストール

% composer require --dev mmo/faker-images

4:Factory に組み込む

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
⭐️use Illuminate\Support\Str;
⭐️use Illuminate\Support\Facades\Storage;
⭐️use Mmo\Faker\PicsumProvider;
⭐️use Mmo\Faker\UnsplashProvider;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Item>
 */
class ItemFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        // `faker` に PicsumProvider を追加
        ⭐️$this->faker->addProvider(new PicsumProvider($this->faker));

        // 1️⃣ 一時ディレクトリに画像をダウンロード
        ⭐️$tempPath = $this->faker->picsum(null, 640, 480); // `null` で一時フォルダに保存

        ⭐️if (!$tempPath) {
            throw new \Exception("画像が生成されませんでした。ディレクトリの権限を確認してください。");
        }

        // 2️⃣ `storage/app/public/items/` に保存
        ⭐️$storagePath = 'items/' . Str::random(10) . '.jpg';
        ⭐️Storage::disk('public')->put($storagePath, file_get_contents($tempPath));

        return [
            'name' => $this->faker->word(),
            'expiration_type' => $this->faker->boolean(),
            'deadline' => $this->faker->date(),
            'comment' => $this->faker->realText(10),
            ⭐️'image_path' => $storagePath, // ✅ `storage/items/` に保存されたパス
            'user_id' => 1,
        ];
    }
}

5:seederでcallする

user_idがある場合は、Userも必ず行う

/Applications/MAMP/htdocs/Laravel/EXP-alert/database/seeders/DatabaseSeeder.php
<?php

namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call([
            ⭐️UserSeeder::class,
            ItemSeeder::class,
        ]);


        // \App\Models\User::factory(10)->create();
        ⭐️\App\Models\Item::factory(2)->create();

        // \App\Models\User::factory()->create([
        //     'name' => 'Test User',
        //     'email' => 'test@example.com',
        // ]);
    }
}

6:php artisan migrate:fresh --seed

% php artisan migrate:fresh --seed

1️⃣ 全てのテーブルを削除(DROP TABLE)
2️⃣ データベースの最新マイグレーションを適用(migrate)
3️⃣ Seeder を実行して、テストデータを挿入(db:seed)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?