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

Ranx99's avatar

How to keep old model's data until new changes are approved ??

There is a model called Book, a book can have many chapters ( one-to-many relationship ) each chapter has many sections ( one-to-many relationship ).

class Book extends Model
{
    public function sections()
    {
        return $this->hasMany('App\Section');
    }
}
class Section extends Model
{
    public function chapters()
    {
        return $this->hasMany('App\Chapter');
    }
}

And lets say I have this Scenario:

1 - user "A" has created a book ( title, desc, sections).

2 - user "A" asked for an approval from admin. then admin approve user "A" 's book.

3 - user "A" decided to delete a book's section, edit a section's chapter or maybe edit the book's title.

4 - then user "A" asked for a an approval from admin for these changes.

and then the admin can approve or disapprove these changes:

1 - if admin approve , apply the new changes.

2 - if admin disapprove , don't apply new changes. keep old data ( with its old relations data ( sections and chapters ).

How to accomplish something like this? is this possible?

0 likes
10 replies
Vilfago's avatar

Maybe with another table and model BookChange with a column for the type of change (delete or update) and a nullable field with a json with the new book after change (in case of update) and a book_id.

Until admin approves, no change are considered in the Book model, and when approved, you retrieve the json, update Book with it and delete the BookChange.

1 like
Ranx99's avatar

Lets say the user deleted a section ( which is a relation linked to book ) from this book.. or edited it..

Where does book's "sections" old data are stored?

Vilfago's avatar

I didn't think about it, but I think you still can store it in a json object.

martinbean's avatar

@Ranx99 You could create a new model, say SectionRevision. A Section would have many SectionRevisions. When a SectionRevision is approved, it could copy its changes to the related Section model, so your Section model always has the last-approved revision’s content.

1 like
Tray2's avatar

I would make it simple and add another column to the table like "approved" then I would get the latest record that is approved and as soon as any new record is approved it will be the latest. I would not delete the old records unless they grow in number very rapidly.

Ranx99's avatar

so, I will create tow tables? something like: BookRevision and SectionRevision ?? this seems too complicated to do .. since a book will have many relations like ( sections - tags - category ) etc.. then I would create a table for each one ?

I think I could use morph relation to so I can have only one table for each one ( maybe call it 'revisionable' ) ??

there must be a package that does this.. I need to search more..

Ranx99's avatar

@TRAY2 - Sorry, but I don't understand how does this solve the problem. can you explain more?

Tray2's avatar

A section is submitted and awaiting approval.

The admin approves the section and it's marked as approved.

An update of previous section is submitted into a new not approved record.

The admin then approves the section and it's marked as approved.

The page displays the lates approved version (version 2) version 1 is still in the table but is filtered out since it's not the latest version.

A third submission of the section is made and awaits approval.

The admin decides not to approve the section thus not marking it as approved.

The page still shows version 2.

The rejected section is updated (this should be possible with not approved sections).

The admin then approves the updated version 3

The page displays version 3.

Think of it as a version handling system.

Please or to participate in this conversation.