In this section, we will see how to generate fake image data in Laravel 9. We’ll generate dummy images and customize their dimensions, including height and width.
Step 1: Create Laravel Project
To install a fresh Laravel application, navigate to your 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 need to connect the Laravel app to the database. Open the .env configuration file and add the database credentials as suggested below.
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
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",
},
],
}