As with the Faker library, you can produce fake data for your database tables, which comes really handing in the development phase of your application when you don't have the actual data.

With Faker library, you can also create fake images.

Here is how I was able to it with Faker in Laravel.

Consider having a Product model where every product is supposed to have an image. I have created ProductFactory wherein you can use $faker->image function to generate random images for you.

Make sure you run the following the command from your terminal at your project

php artisan storage:link

<?php 
/* @var $factory \Illuminate\Database\Eloquent\Factory */ 
use App\Product; 
use Faker\Generator as Faker; 
$factory->define(Product::class, function (Faker $faker) {
    return [
        'name' => $faker->word,
        'short_description' => $faker->sentence,
        'description' => $faker->paragraph,
        'category_id' => function () {
            return factory(App\Category::class)->create()->id;
        }, 
        'amount' => $faker->randomFloat(2, 0, 10000),
        'image' => $faker->image('public/storage/images',640,480, null, false),

    ];
});

Now when you run your factory method from tinker or Seed class, it will generate a random image for you and will store it in public/storage/images directory.

This is how you can reference the images on the front-end

<img src="/storage/images/{{$product->image}}">
Comments