Support the ongoing development of Laravel.io →
Eloquent Laravel

I’m struggling to get the relations right for some reason on some tables I’m trying to setup today. I’m building an app which allows posting of articles and maintaining older versions. My intent is to have an ‘articles’ and ‘article_versions’ table. The article_versions table will have an article_id table that points to articles.id.

My thinking is I want to add a current_version_id on the articles table, but I'm not sure how I should define that relationship. As of now I have an article that belongsTo article_versions. This muddles up my creation process though. As of now I'm overloading save() to do the following:

  1. Save the article
  2. Create a new version and save it
  3. Update the current_version_id on the article table and saving it

This mostly works but feels pretty kludgey. Ideally these are the final things I want to achieve:

  1. An article should have multiple versions stored
  2. An article should be able to be reverted to an older version

Any thoughts or suggestions would be appreciated.

Last updated 3 years ago.
0

Hi trahma,

interesting problem. From your question is not clear to me where the content of a specific article version is stored. I think two tables are enough. Let's keep the table names article and article_version. I would define them as article - id, title, ... article_version - article_id, version, creation_time, content article hasMany article_versions. It makes sense to me also to have a 'relationship' where you only take the version names without the content.

Hope this helps

0

Sign in to participate in this thread!

Eventy

Your banner here too?

trahma trahma Joined 8 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.