0

In my project I have a dynamic form of the following structure:

- Date 1
  - Shift 1
    - Area 1
      - Cell 1
      - Cell 2
    - Area 2
      - Cell 3
      - Cell 4
  - Shift 2
    - Area 1
      - Cell 1
  ...

I take data from inputs with the following multidimensional array: data[index][column], where a column may be: date, shift, area, cell, or cell content.

Then I need to store this form into database to be able to recreate it later. It's easy, because my array has a table form:

index | Date  | Shift | Area | Cell | Cell Content
1       05.06   1       1       1         100
2       05.06   1       1       2         99
3       05.06   1       2       3         55
4       05.06   1       2       4         66
5       05.06   2       1       1         35

But when to recreate the form (take it back from the database), it would be better to convert this array into a collection, to be able to do the following foreach:

foreach (dates as date)
  foreach (date->shifts as shift)
    foreach (shift->areas as area)
      foreach (area->cells as cell)
         echo cell->content
      endforeach
    endforeach
  endforeach
endforeach

Is it possible in Laravel? Or I need to use the old method: comparison current element with precedent and break the for when different?

1 Answer 1

1

The problem I see here is that all the columns are stored in the same table i.e. you have a denormalized structure. A simple solution would be to normalize your data (might come with a cost) and then defining the relations would be very simple in Laravel.

So I suggest you split your single table into :

  • dates (id, date)
  • shifts (id, date_id, shift)
  • areas (id, shift_id, area)
  • cells (id, area_id, cell)

Now you can also choose to have redundant data and add shift_id and date_id to your cell table. If saving space is not your concern, maybe store data in both the ways.

Read more about normalization https://www.geeksforgeeks.org/database-normalization-normal-forms/

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, u are right. I have redundant data in this table. Need to think more bout it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.