I am trying to save the image using faker in the storage folder. I want to keep the images inside the property ID folder. I have tried the below code but it is not working. It hangs the command prompt. The properties have multiple images and are a one-to-many relation. The code below is for the PropertyImageFactory class.
public function definition()
{
$property_id = $this->create(Property::class)->id;
return [
'name' => $this->faker->image(storage_path('app/property-images/'.$property_id), 200, 200, 'cats'),
'sort_order' => $this->faker->numberBetween(1, 10),
'created_at' => now(),
'updated_at' => now(),
];
}
Laravel documentation has 'user_id' => User::factory(),
I can’t use this because it returns an object and I can’t concatenate to string.
Below is the code for the property seeder.
public function run()
{
Property::factory()
->has(Category::factory()->count(3))
->has(PropertyImage::factory()->count(3), 'images')
->count(5)
->create();
}
2
Answers
Yup, that will return a
UserFactory
object just asProperty::factory()
will return aPropertyFactory
. What you want to do is call thecreate()
method on the factory and get theid
from the record that is created.Laravel extracts the ID from the
SomeModel::factory()
, that’s all the magic of Laravel. To do this, you need to properly describe theSomeModelFatory
. You can use:$property_id = Property::factory();
As far as I know
faker->image
no longer works, I can advise using an auxiliary class for image formation.and use: