ajfick
1 month ago

Splitting model data between tables based on type

Posted 1 month ago by ajfick

Hello!

I'm working on a listing site which will have a few major categories of listings available to users. These categories will each have some "listing type" specific information. For example one major listing type may be "Vehicles" which needs to have make, model and year, where another major listing type for "Clothing" would need size and brand.

I'm wondering if there would be a good way to do this where I store the shared listing information in a listings table, and the listing type specific information in another table like vehicle_listings or clothing_listings with a foreign key of listing_id which points to the id column on listings.

I'm wondering if there is a good way to do this using eloquent that would allow for easy retrieval of the data from both columns into the model without needing to manually request the listing type specific data through DB::table('vehicle_listings') or something like that. I'd rather be able to do something like $listing->typeData()->model if possible.

My questions are: Is there a built in way to do this sort of thing? And Is there a design pattern that may help me with this implementation?

Any advice will be greatly appreciated!

Please sign in or create an account to participate in this conversation.