I'm working on a business website with Laravel. I'm trying to generate fake images with a factory to make tests easier, but it's not working because I don't know how to add the path of my images in the code.
The code which generates fake data in database:
use App\Product;
use Faker\Generator as Faker;
$factory->define(Product::class, function (Faker $faker) {
// $images = [];
// $images[0] = '/img/fashion/1.jpg';
// $images[1] = '/img/fashion/2.jpg';
// $images[2] = '/img/fashion/3.jpg';
// $images[3] = '/img/fashion/4.jpg';
return [
'category_id' => $faker->numberBetween($min = 1, $max = 3),
'brand_id' => $faker->numberBetween($min = 1, $max = 3),
'name' => $faker->word,
'url' => $faker->word,
'description' => $faker->sentence($nbWords = 6, $variableNbWords = true),
'price' => $faker->numberBetween($min = 1, $max = 20),
'content' => $faker->realText($faker->numberBetween(10, 20)),
'image' => $faker->image('public/storage/images', 640, 480, null, false)
];
});
I saw a training video on YouTube in which we attach media information to a Laravel application via the package spatie/laravel-medialibrary, but it's not clear.
Can anyone please explain this?
You are creating the image using faker in the correct way.
You will need to run the following command:
php artisan storage:link
Following that, you can access the image using:
<img src="/storage/images/{{$product->image}}">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With