how to generate fake image in laravel 9

In this section we will see how to create image fake data in laravel 9. We will create image fake data and also set fake image size height & width.


Step 1: Create Laravel Project

Installing a fresh new laravel 9 application, so head over to the terminal, type the command, and create a new laravel app.

composer create-project laravel/laravel laravel-faker


Step 2: Set Up Database Details in ENV

Now, you have to connect the laravel app to the database, hence open the .env configuration file and add the database credentials as suggested below.

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_user_name
DB_PASSWORD=database_password


Step 3: Create Image Modal Migration and Factory

Create Image Modal with migration

php artisan make:model Image -m

Create Image Factory

php artisan make:factory ImageFactory --model=Image


Shorthand

You can simply create Modal with migration & Factory in single command.

php artisan make:model Image -mf


2022_09_16_180045_create_images_table.php

public function up()
    {
        Schema::create('images', function (Blueprint $table) {
            $table->id();
            $table->string('image');
            $table->timestamps();
        });
    }


Step 4: Add Image fake data method in image Factory

Add image fake()->imageUrl() to create fake image placeholder.

database/factories/ImageFactory.php

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Image>
 */
class ImageFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'image' => fake()->imageUrl(),
        ];
    }
}

You can also set fake image size height and width.

'image' => fake()->imageUrl($width=400, $height=400)

Import Image Model in DatabaseSeeder.php to create image fake data

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()
    {
        \App\Models\Image::factory(10)->create();
    }
}


Run migrate with seeder

php artisan migrate:fresh --seed  


Step 5: Check Image Fake data via laravel tinker

Now check image fake data in laravel tinker.

php artisan ti 


Psy Shell v0.11.8 (PHP 8.1.3  cli) by Justin Hileman
>>> App\Models\Image::all()
=> Illuminate\Database\Eloquent\Collection {#4566
   all: [
    App\Models\Image {#4568
     id: 1,
     image: "https://via.placeholder.com/640x480.png/004433?text=quibusdam",
     created_at: "2022-09-16 18:03:12",
     updated_at: "2022-09-16 18:03:12",
    },
    App\Models\Image {#4569
     id: 2,
     image: "https://via.placeholder.com/640x480.png/00ff55?text=enim",
     created_at: "2022-09-16 18:03:12",
     updated_at: "2022-09-16 18:03:12",
:...skipping...
=> Illuminate\Database\Eloquent\Collection {#4566
=> Illuminate\Database\Eloquent\Collection {#4566
=> Illuminate\Database\Eloquent\Collection {#4566
   all: [
    App\Models\Image {#4568
     id: 1,
     image: "https://via.placeholder.com/640x480.png/004433?text=quibusdam",
     created_at: "2022-09-16 18:03:12",
     updated_at: "2022-09-16 18:03:12",
    },
    App\Models\Image {#4569
     id: 2,
     image: "https://via.placeholder.com/640x480.png/00ff55?text=enim",
     created_at: "2022-09-16 18:03:12",
     updated_at: "2022-09-16 18:03:12",
    },
    App\Models\Image {#4570
     id: 3,
     image: "https://via.placeholder.com/640x480.png/0000ee?text=sunt",
     created_at: "2022-09-16 18:03:12",
     updated_at: "2022-09-16 18:03:12",
    },
    App\Models\Image {#4571
     id: 4,
     image: "https://via.placeholder.com/640x480.png/009900?text=dolores",
     created_at: "2022-09-16 18:03:12",
     updated_at: "2022-09-16 18:03:12",
    },
    App\Models\Image {#4572
     id: 5,
     image: "https://via.placeholder.com/640x480.png/0099ff?text=sapiente",
     created_at: "2022-09-16 18:03:12",
     updated_at: "2022-09-16 18:03:12",
:...skipping...
=> Illuminate\Database\Eloquent\Collection {#4566
   all: [
    App\Models\Image {#4568
     id: 1,
     image: "https://via.placeholder.com/640x480.png/004433?text=quibusdam",
     created_at: "2022-09-16 18:03:12",
     updated_at: "2022-09-16 18:03:12",
    },
    App\Models\Image {#4569
     id: 2,
     image: "https://via.placeholder.com/640x480.png/00ff55?text=enim",
     created_at: "2022-09-16 18:03:12",
     updated_at: "2022-09-16 18:03:12",
    },
    App\Models\Image {#4570
     id: 3,
     image: "https://via.placeholder.com/640x480.png/0000ee?text=sunt",
     created_at: "2022-09-16 18:03:12",
     updated_at: "2022-09-16 18:03:12",
    },
    App\Models\Image {#4571
     id: 4,
     image: "https://via.placeholder.com/640x480.png/009900?text=dolores",
     created_at: "2022-09-16 18:03:12",
     updated_at: "2022-09-16 18:03:12",
    },
    App\Models\Image {#4572
     id: 5,
     image: "https://via.placeholder.com/640x480.png/0099ff?text=sapiente",
     created_at: "2022-09-16 18:03:12",
     updated_at: "2022-09-16 18:03:12",
    },
    App\Models\Image {#4573
     id: 6,
     image: "https://via.placeholder.com/640x480.png/000022?text=soluta",
     created_at: "2022-09-16 18:03:12",
     updated_at: "2022-09-16 18:03:12",
    },
    App\Models\Image {#4574
     id: 7,
     image: "https://via.placeholder.com/640x480.png/00cc44?text=velit",
     created_at: "2022-09-16 18:03:12",
     updated_at: "2022-09-16 18:03:12",
    },
    App\Models\Image {#4575
     id: 8,
     image: "https://via.placeholder.com/640x480.png/00ff00?text=dicta",
     created_at: "2022-09-16 18:03:12",
     updated_at: "2022-09-16 18:03:12",
    },
    App\Models\Image {#4576
     id: 9,
     image: "https://via.placeholder.com/640x480.png/00eedd?text=eligendi",
     created_at: "2022-09-16 18:03:12",
     updated_at: "2022-09-16 18:03:12",
    },
    App\Models\Image {#4577
     id: 10,
     image: "https://via.placeholder.com/640x480.png/001199?text=ratione",
     created_at: "2022-09-16 18:03:12",
     updated_at: "2022-09-16 18:03:12",
    },
   ],
  }


Read Also

Laravel 9 Upload Multiple Image Using Spatie Media Library

How To Upload Multiple Images In Laravel 9 With Intervention

Laravel 9 Upload Multiple Images Tutorial Example

How to update multiple images in laravel 9

Upload Images with Spatie Media Library in Laravel 9