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

Cbrad24's avatar

Dynamic Checklist Problem

Hey guys,

Trying to generate a dynamic checklist, and running into issues where some of it has ended up being hard coded rather then dynamic due to not being able to figure some steps out.

Here's an image for context: https://imgur.com/a/8moMuhP

Here is the dynamic array which sits in the Checklist model:

    const checklist = [
        [
            'label' => 'Test 1',
            'value' => 't1',
            'desc'  => TRUE
        ],
        [
            'label' => 'Test 2',
            'value' => 't2',
            'desc'  => TRUE
        ],
        [
            'label' => 'Test 3',
            'value' => 't3',
            'desc'  => TRUE
        ],
        [
            'label' => 'Test 4',
            'value' => 't4',
            'desc'  => TRUE
        ],
        [
            'label' => 'Subchecklist 1',
            'value' => 's1',
            'desc'  => TRUE,
            'sub'   => [
                [
                    'label' => 'Sub1',
                    'value' => 'sb1'
                ],
                [
                    'label' => 'Sub2',
                    'value' => 'sb2'
                ],
            ],
        ],
        [
            'label' => 'Test 5',
            'value' => 't5',
            'desc'  => TRUE
        ],
    ];

So using the value to link it back to the database dynamically, I was able to get most things working. The biggest thing I was struggling with was, because when a user checks a checkbox, it records their User ID from the Users table.

Because I need to poll the User's name from the model, I ended up entering a lot of the below into the model for each relationship.

    public function t1User() {
        return $this->hasOne( 'App\User', 'id', 't1_id' );
    }

    public function t2User() {
        return $this->hasOne( 'App\User', 'id', 't2_id' );
    }

I have tried to dynamically generate the relationships using something like this but haven't gotten anywhere yet. I am starting to exhaust all my options as I can't seem to find any examples on dynamic relationships.

    public function __get( $key ) {
        foreach ( self::checklist as $item ) {
            if ( $item['value'] . 'User' === $key ) {
                return $this->hasOne( 'App\User', 'id', $item['value'] . '_id' );
            }
        }

        return parent::__get( $key );
    }

I was also thinking, that this might be causing a lot of redundant SQL lookups as each checkbox is going to do a lookup on a user when checked. The reason I really need to get this sorted is our security team has approached me now saying they want to add some 30-40 extra checks for their department so it would end up being a disaster to hard code something like this.

I suspect my approach may just be completely off, maybe I should be fetching a list of users and matching them to every checkbox entry, but I was hoping someone more experienced might be able to point me in the right direction.

Edit: The database is a single row for each 'day' on the checklist, with a column for each checkbox, eg t1_id, t2_id, t3_id, etc.

Any help is definitely appreciated.

Thanks, Corey

0 likes
0 replies

Please or to participate in this conversation.