Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PeregrineStudios's avatar

'Extending' Database Tables?

Let's suppose I have three different types of file uploads - each with their unique attributes, each used very exclusively of the others (for example, let's say type 1 is user-uploaded files for a media library of some kind, type 2 is application-generated files for a specific module, and type 3 is is files automatically downloaded from an FTP server by a cron). These files all share some common database fields - id, disk, and path come to mind. But many other attributes might be unique to each type - like user_id or ftp_server.

If I were writing these as PHP classes, I might write a base File class, that MediaFile, CronFile, and FtpFile would extend. However, I don't believe there's any way to accomplish a similar structure using databases and Eloquent models, unless I'm mistaken and one of Laravel's many, many arcane features includes such a setup?

0 likes
2 replies
austenc's avatar
austenc
Best Answer
Level 10

One strategy you could consider would be an uploads table with a couple polymorphic fields like so:

uploads
 - id
 - attachable_id
 - attachable_type

This way you could have separate tables/models for any type of upload you need. Another approach might be to use the json mysql field type to store that type of extra information as a json string.

1 like
PeregrineStudios's avatar

Late to reply, but that was exactly what I needed, thanks! I had completely forgotten polymorphic was a thing.

Please or to participate in this conversation.