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

aurelianspodarec's avatar

Laravel - Structuring data with same columns but different tables

Hi there!

So I've been working on a project and its been alright so far, but now I feel like the data structure might need some improvements.

The way this should work:

Company contains company information, be it a name, brand color, established date and so on. The user will be able to filter the Companies by color, industry, etc...

Every company, will have a Pages and Components. These will also have their own URL, that will get all company pages and display them - which then can be filtered by things like page name (which is what Page contains) but also by company color.

Now, I do also want to add independent pages, without companies. Would I then create a new table Pages and Components and rename the other ones to company_pages and company_components and then join them to show all of them? And when user wants to filter to see only page associated with company they can.

And the Pages then would also consist of everything that the company page has, color etc... since if I want to insert a page by its own, and give it a color, that wouldn't be possible unless its associated with a company.

So I feel like this data structure could be good:

company
- id
- color
- name

company_pages
- id 
- company_id
- image
company_components
- company_id
general_pages
- id
- color
- name
- image

And same for components

And then join these two tables when displaying the data and that way if the user wants to filter by color, they all can be filtered.

What do you guys think? Or is this a wrong way to look at this? Because I don't see a reason to make all pages to have color since its going to be exactly the same as the company has so it would be a massive 1000 duplicate records for each company. So just the company should have the color I'm sure. But then those pages that aren't associated with the company, have their own table.

I suppose if I do it like that I need to make sure it can be displayed in one component as well.

So far this is what I got https://i.imgur.com/OOpqta6.png but I feel like this sint' flexible enough and naming could be possibly improved.

0 likes
5 replies
aurelianspodarec's avatar

@vincent15000 Not exactly.

I wonder how I should structure the database.

I have a company, which will have a page, the page will have different variations here only one variation will display. The company will have things like brand colors, unique to the brand of the company, and if you want to get all pages that are red, I willl query the company to check if the company brand is red.

However, when I want to add a non-company page, where would I give the page brand color? Would I then create a new general_pages database and then join the company_pages and general_pages?

I'm trying to structure database for my applicatoin at this point. I don't think polimorphics relationship is good here because the data will be difference, since a company and anything for a company will be only for the company, and non company pages will have all the columns as the company+company pages, just need to figure out how to store it.

aurelianspodarec's avatar

@vincent15000 I suppose, in other words

The application will be able to display companies, pages and components.

The pages are associated with pages and the companies are associated with companies possibly with pages as well in the future.

The company will have things like the company name, brand colors etc.... The page will have things like page name as well as, four different versions of the page, such as is_dark and have an image there, so that would give us two variations - up to four differen variations for a page.

However, what if I want to add an independent page, without a company, but still retain the color, brand name etc... ?

Or would I in that case just put evertyhing as a 'company' and then add a column is_proper_company to see if it is or?

Struggling on how I should do the data.

I think there might be a place for polymorphic relationships somewhere though.

Maybe the polimorphic relationship could be for page and page content or the company or somethng, not sure.

Atef95's avatar

I don't know if I have correctly understand what you want to achieve.. I guess you can keep Page model as it is and set company_id nullable.. so it's like 0 to many relationship..

Please or to participate in this conversation.