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
Spatie's Media Library?
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
That's how it works, polymorphic relationship to your models
Yep makes complete sense now, I was just fixated on the name and images I've seen of the plugin hehe
Haha nope, perfectly fine for any type of files. Infact I used it with PDFs and ZIPs as well
Perfecto
Thanks!
And as a bonus with the official filament plugins for it, makes it effortless to use it along with filament
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?
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.
You could also use https://spatie.be/docs/laravel-medialibrary/v10/basic-usage/retrieving-media
Retrieving media | laravel-medialibrary
laravel-medialibrary
Just give it a try and lemme know if you get stuck or need any help π
Cheers bud!
Hey Zerox, how do you set the directory path within S3? I've tried directory() but that doesn't seem to work.
Sorry i haven't worked with S3
No problem, I've just simplified the mapping and using more disks intead to achieve a result that is workable.