jcc5018's avatar

Is there a github like solution to keep track of changes within a model content?

I would like to find something similar to Github that only stores the changed content values and keeps tracks of revisions of my articles. Ideally something where someone could go back and review all the versions of the articles (perhaps with expirations or limits to number of versions). Also, potentially track who made each revision if used on something like a "terms of service" where multiple people may be able to make edits over time.

I'm thinking that if the system only stores the changed part, then that would take up less space in the database. But if this is too complicated, I can just store the whole article for now.

Is this something Laravel can handle alone, or would this be solution for another language? Are there any packages already built for this task?

Thanks

0 likes
4 replies
jlrdw's avatar

You should be able to create another database or perhaps another table and write a database trigger to work this out.

vtalbot's avatar

There is a package by Spatie that you could use to log all activities. It stores who performed the changes, previous values and new values.

jcc5018's avatar

@vtalbot Thanks, I was looking at that package, as well as versionable, and laravel auditing. They all look fairly similar, but i dont want to clutter up my code trying all 3. So I am wondering if you all have experience with the pros and cons of these packages.

Again my main desires at this point, is to record changes to long form content, and perhaps even allow it to be highlighted so I can use it similarly to Microsoft words change tracker. (users can accept or deny a change)

The auditing aspect may also be useful for other models, so I'm wondering if these packages can work together if one doesnt handle what I am looking for.

LaryAI's avatar
Level 58

Certainly! You can achieve version control for your model content in Laravel using the spatie/laravel-activitylog package or the venturecraft/revisionable package. Both packages allow you to track changes to your models, including who made the changes and what was changed.

Using spatie/laravel-activitylog

The spatie/laravel-activitylog package is a powerful tool for logging model changes. Here’s how you can set it up:

  1. Install the package:

    composer require spatie/laravel-activitylog
    
  2. Publish the configuration file:

    php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-config"
    
  3. Add the LogsActivity trait to your model:

    use Spatie\Activitylog\Traits\LogsActivity;
    
    class Article extends Model
    {
        use LogsActivity;
    
        // Define which attributes you want to log
        protected static $logAttributes = ['title', 'content'];
    
        // Optionally, you can customize the log name
        protected static $logName = 'article';
    
        // Optionally, you can customize the description of the log
        public function getDescriptionForEvent(string $eventName): string
        {
            return "Article has been {$eventName}";
        }
    }
    
  4. Retrieve the activity log:

    $activities = Activity::where('log_name', 'article')->get();
    

Using venturecraft/revisionable

The venturecraft/revisionable package is another excellent option for tracking model changes. Here’s how to set it up:

  1. Install the package:

    composer require venturecraft/revisionable
    
  2. Publish the configuration file and migration:

    php artisan vendor:publish --provider="Venturecraft\Revisionable\RevisionableServiceProvider"
    php artisan migrate
    
  3. Add the RevisionableTrait to your model:

    use Venturecraft\Revisionable\RevisionableTrait;
    
    class Article extends Model
    {
        use RevisionableTrait;
    
        // Optionally, you can specify which attributes to track
        protected $revisionable = ['title', 'content'];
    }
    
  4. Retrieve the revisions:

    $article = Article::find(1);
    $revisions = $article->revisionHistory;
    

Conclusion

Both packages provide robust solutions for tracking changes to your model content. If you need more advanced features like diffing content or handling large datasets efficiently, you might need to implement a custom solution or extend these packages.

For a simple and quick setup, spatie/laravel-activitylog is highly recommended due to its flexibility and ease of use. If you need more detailed revision history, venturecraft/revisionable might be the better choice.

Feel free to choose the one that best fits your needs!

Please or to participate in this conversation.