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

uniqueginun's avatar

Structuring database table that needs approval for each update

Hello there

I'm trying to come up with the best structure for database table which the business requires approval for each update/delete action.

so when new data is entered by system user it doesn't show in the system unless approved by this user's moderator also any update/delete.

here's what I think

ideas table:
#id
#title
#desc
#status_id
#created_by
#updated_by
#action_type ===> create-update-delete
#action_date
#action_info => JSON field to store updates temporarily 
#timestamps

so when moderator rejects the action I just clear out actions fields otherwise I update table according to action_info field. if action is delete I just delete the record.

But I think there is a BETTER way to do it don't you think?

0 likes
4 replies
chaudigv's avatar

To keep it simple, this is same as a status field i.e. active, banned, pending.

Having appropriate where condition will fulfill your requirements.

martinbean's avatar
Level 80

@uniqueginun To be flexible, you can have a single table (called “pending updates” or something) that has a polymorphic relation pointing to the model being updated, and then the changed serialised in a JSON column or something. You can then create a controller and view that just paginates through the rows in the table for an admin to approve. If a change is approved, it’s applied to the target model:

class PendingUpdate extends Model
{
    protected $casts = [
        'changes' => 'json',
    ];

    public function target()
    {
        // Polymorphic relationship pointing to model being updated
        return $this->morphTo();
    }

    public function approve()
    {
        // Update model with requested changes
        $this->target->update($this->changes);

        // Delete this pending update instance
        $this->delete();
    }

    public function reject()
    {
        // Just simply delete the model if the change is rejected
        $this->delete();
    }
}

You can then have a controller that calls these model methods to approve/reject a proposed update:

$pendingUpdate->approve();
uniqueginun's avatar

well this is good Idea but I wont use morph relation because the updates related to one single model. I will create this PendingActions model and make it one to one relationship with my targeted table and create these fields:

#id
#target_id
#action_type
#changes
#created_by
#timestamps

Thank you. this makes more sense

1 like

Please or to participate in this conversation.