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

mdev11's avatar

Edit model with all related tables

I have a table for comments, each comment could have a decision from a dropdown. If the specific decision is selected the user then could select one/many services. If a specific service is selected only one grade is selected.

So the result comments section could look like these examples:

This is a comment without a decision

This is a comment with a decision
decision: This is decision 1

This is a comment with a decision and services
decision: This is decision 2
services: ['This is service 1', 'This is service 2']

This is a comment with a decision, services, and grade
decision: This is decision 2
services: ['this is service 1', 'This is service 3 grade: 2']

The decision is only a single selection. The services are multiple checkboxes. The grade is only a single selection.

The form looks like that:

   <form>
    <textarea name="comment"></textarea>
    <select name="decision">
        <option value="1">Decision 1</option>
        <option value="2">Decision 2</option>
    </select>
    <!-- Show if decision 2 is selected -->
    <div class="services">
        <input type="checkbox" name="services[]" value="1"/>
        <input type="checkbox" name="services[]" value="2"/>
    </div>
    <!-- Show if service 2 is checked -->
    <select name="grade">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
</form>

DB structure:

comments table:

id | comment
1  | This is comment 1
2  | This is comment 2

decisions table:

id | decision

1  | This is decision 1
2  | This is decision 2

services table:

id | service
1  | This is service 1
2  | This is service 2

grades table:

id | grade
1  |   1
2  |   2
3  |   3
4  |   4

Finally, these tables are related using 3 tables:

comment_decisions table:

id | decision_id | comment_id

comment_services table:

id | service_id | comment_id

comment_grades table:

id | comment_services_id | grade_id

I did that to avoid duplication and NULL values in the case of one table:

id | comment | decision_id (nullable) | service_id (nullable) | grade_id (nullable)

This issue is when I try to update the decision, services, and grade. When I use updateOrCreate new records are created not updated. attach and sync not working either.

Is there a better structure or I'm missing the right functions to update the tables?

0 likes
6 replies
Lara_Love's avatar

Hello. You say that if this is chosen, do this. If it is selected, do it?

mdev11's avatar

@LoverToHelp Services and grade fields are hidden. I show them with Javascript if specific decision/service selected

Lara_Love's avatar

the structure your update form and controller

Please or to participate in this conversation.