I try to seed using has function for relationship but does not create child record.
I can propertyServices separately, it works.
I wonder what I made wrong on using has in PropertySeeder
#factory #seeder
3 Replies
I don’t really understand the question or what the issue is.
First example: I don’t know whether it’s supported to accept
$property
as a param. Where did you find this? I have never seen that syntax.I would raise another question: why you need house_no in both parent and child records? The relationship should be probably by property_services.property_id with hasMany.
And if you do need that house_no in the property_services, you can call it by relationship: property_service.property.house_no
I follow idea from this https://laravel.com/docs/11.x/eloquent-factories
$user = User::factory()
->has(
Post::factory()
->count(3)
->state(function (array $attributes, User $user) {
return ['user_type' => $user->type];
})
)
->create();
In fact, I just want to be easy for searching. User will search on this child property_services, with house_no. If I don't pass this in db, I have to search across table, I think it will be slower.