Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ajithlal's avatar

Faker save image to sub folder under public folder

I've created a faker to generate images. I want to store the images to my works folder under public folder. By default faker is storing images to storage. so I changes the image path using public_apth() function. but it didn't storing images. my code is below:

$factory->define(\App\Models\UserPhoto::class, function (Faker $faker) {
    return [
        'image' => $faker->image(public_path('work_photos'), 400, 300, null, false),
    ];
});
0 likes
4 replies
bobbybouwmann's avatar

Because public_path returns your local public path. That might look like this /www/projects/laravel/public/work_photos. Instead, you need to set a relative path

$faker->image('public/work_photos', 400, 300, null, false),
ajithlal's avatar
ajithlal
OP
Best Answer
Level 18

@bobbybouwmann Thanks for the support. Issue was with the $faker->image() function. $faker->image() function is calling $kaker->imageUrl() function and that function sets $baseUrl variable to lorempixels.com like this$baseUrl = "https://lorempixel.com/";. I replaced https to http and now its working. but I actually don't know why I want ot do this. but its working fine now.

bobbybouwmann's avatar

It's downloading the image from that URL and then puts that locally. Also, it seems that lorempixel switched from http to https again. There was a long time when they didn't have https.

Please or to participate in this conversation.