cosminc's avatar

Saving image in local storage when creating a factory

Hello,

I'm trying to save an image in local storage when creating a factory. My code so far looks like this:

public function definition()
{
    return [
        'venue_id' => Venue::factory(),
        'photo' => $this->faker->image(storage_path('venues/hero'), 1200, 300, null, false)
    ];
}

The problem is that if I try to use this factory I get the following error: InvalidArgumentException with message 'Cannot write to directory "/home/foo/www/laravel/storage/venues/hero"'

From the controller I save the uploaded image a following and it works:

$photo = $request->file('hero_photo')->store('venues/hero', 'public');

I also tried without storage_path() but it didn't work.

What can I do?

Thanks!

0 likes
3 replies
bugsysha's avatar

Create that folder with appropriate permissions.

mkdir -p /home/foo/www/laravel/storage/venues/hero

Read more here. Suggested permissions are 775 or 664.

cosminc's avatar

I want the image to be saved in the same place where it's saved using the controller method.

That would be storage/app/public/venues/hero, so the folder already exists.

Why does it work from the controller without setting additional permission and it doesn't from the factory?

siangboon's avatar

just follow bigsysha steps to create the EXACT directory at the storage and set the permission accordingly first.

obviously "/home/foo/www/laravel/storage/venues/hero" is not same as "storage/app/public/venues/hero"

Please or to participate in this conversation.