Max Krizh
Max Krizh

Reputation: 605

Laravel Eloquent: DB schema design

I am writing a project for, let's say, a company that maintains multiple real estate projects throughout the city. In this way, they have:

  1. Real Estate Project (typically a group of apartment houses with single main address).
  2. Single apartment house in the group (project), that has it's own address.
  3. Single apartment inside the house.

So, the address of #1 will be Main street - 28, address of #2 will be Main street - 28, building 4 and address of #3 will be Main street - 28, building 4, apt. 12.

The problem that they also have some single houses that are actually as #1 and #2 at the same time (like Another street - 123 (and lots of apartments)).

I am struggling to design a DB schema for this and have multiple variants:

  1. Each address, not regarding if it is apt, house or project address, will be stored in own DB row (like plain text):
id address
1 Main street - 28
2 Main street - 28 building 1
3 Main street - 28 building 1 apt. 12

The problem is that since there might not be the middle section (no main address extension, like building 4), I'm afraid the last variant won't work as expected, since each apartment will be attached to it.

Would really appreciate your help!

Kind regards,

Upvotes: 0

Views: 184

Answers (1)

Max Krizh
Max Krizh

Reputation: 605

Found the way this could be achieved. I used one of many laravel packages for Nested Set Model structure implementation. In this case, I will have the tree-like model in database, where there are parents and descendants.

In this way, I can refer to every parent element even if I have retrieved only the deepest child element of the root. This also allows multiple operations, like counting up/down (parents/children), as well as building an actual visualized tree model.

There are multiple Laravel packages for achieving this.

Upvotes: 0

Related Questions