I don't think I have to create a FolderFactory for this
How then do you get valid folder_id for the relationship?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a FileFactory and I made a factory for it. there also a pivot table with a many to many relationship called folder_file with a relationship between the folder and the file.
For my testing I want to update the pivot table with same folder_id and the created file_id from the factory.
So the folder is the same for all the files created.
The pivot table should look like that:
folder_id | file_id
----------|----------
1 | 1
1 | 2
1 | 3
1 | 4
1 | 5
What is the correct way to do that? I don't want to create records in the folders table because it's only a single folder right now that is created when I seed the db.
I don't think I have to create a FolderFactory for this although it gives me errors when I try to use the has method: https://laravel.com/docs/9.x/eloquent-factories#many-to-many-relationships
Any better way to do it?
ty
@Ligonsker if you already have the Folder instance; then:
$folder->files()->attach(File::factory(5)->create());
You can shortcut the Folder and FIles creation in a single expression using:
$folder = Folder::factory()
->hasAttached(File::factory()->count(5))
->create();
Please or to participate in this conversation.