F
Filamentβ€’16mo ago
toeknee

Files to a relationship?

What's the recommended approach to storing files in a database table? We upload to S3, but I want to have relationships linked so a user can see all their files etc. So I'm looking for a generalised files table. I know I could handle this on the post save action for each file, but I was hoping there is a better method / relationship. There is storeFileNamesIn but that's against the same database so just stores them against the same record.
14 Replies
ZedoX
ZedoXβ€’16mo ago
Spatie's Media Library?
toeknee
toekneeOPβ€’16mo ago
I wondered about that, but I don't want a media library really. These are personal documents that are pdf, zip, word etc that need to be contained into a single table Ok jsut done some reading, looks like it would do the trick
ZedoX
ZedoXβ€’16mo ago
That's how it works, polymorphic relationship to your models
toeknee
toekneeOPβ€’16mo ago
Yep makes complete sense now, I was just fixated on the name and images I've seen of the plugin hehe
ZedoX
ZedoXβ€’16mo ago
Haha nope, perfectly fine for any type of files. Infact I used it with PDFs and ZIPs as well
toeknee
toekneeOPβ€’16mo ago
Perfecto Thanks!
ZedoX
ZedoXβ€’16mo ago
And as a bonus with the official filament plugins for it, makes it effortless to use it along with filament
toeknee
toekneeOPβ€’16mo ago
perfecto, I'll swap out the fileupload plugin πŸ™‚ Have you done it where by you link the uploads to a model, but also link them to a user? Example the user will be able to login and see all their files?
ZedoX
ZedoXβ€’16mo ago
I haven't specifically done that, but you can just query the relationship. https://laravel.com/docs/10.x/eloquent-relationships#querying-morph-to-relationships
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation β€” freeing you to create without sweating the small things.
ZedoX
ZedoXβ€’16mo ago
Just give it a try and lemme know if you get stuck or need any help πŸ™‚
toeknee
toekneeOPβ€’16mo ago
Cheers bud! Hey Zerox, how do you set the directory path within S3? I've tried directory() but that doesn't seem to work.
ZedoX
ZedoXβ€’16mo ago
Sorry i haven't worked with S3
toeknee
toekneeOPβ€’16mo ago
No problem, I've just simplified the mapping and using more disks intead to achieve a result that is workable.
Want results from more Discord servers?
Add your server