PHP code example of vildanbina / laravel-versions

1. Go to this page and download the library: Download vildanbina/laravel-versions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vildanbina / laravel-versions example snippets



use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddDraftsColumnsToPostsTable extends Migration
{
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->versionables();
        });
    }

    public function down()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->dropVersionables();
        });
    }


return [
    'column_names' => [
        'is_current' => 'is_current',
        'is_published' => 'is_published',
        'published_at' => 'published_at',
        'uuid' => 'uuid',
        'publisher_morph_name' => 'publisher',
    ],

    'auth' => [
        'guard' => 'web',
    ],
];
~~~

You can customize these settings as needed.

## Getting Started

Follow these steps to set up versioning for your models:

1. **Install the package via Composer**:

    ~~~bash
    composer lumnsToPostsTable extends Migration
    {
        public function up()
        {
            Schema::table('posts', function (Blueprint $table) {
                $table->versionables();
            });
        }

        public function down()
        {
            Schema::table('posts', function (Blueprint $table) {
                $table->dropVersionables();
            });
        }
    }
    ~~~

   Then run:

    ~~~bash
    php artisan migrate
    ~~~

4. **Update your model**:

   Implement the `Versionable` interface and use the `HasVersions` trait:

    ~~~php
    

    namespace App\Models;

    use Illuminate\Database\Eloquent\Model;
    use VildanBina\LaravelVersions\Contracts\Versionable;
    use VildanBina\LaravelVersions\Concerns\HasVersions;

    class Post extends Model implements Versionable
    {
        use HasVersions;

        // Optionally, define excluded columns
        protected array $excludedColumns = ['created_at', 'updated_at'];
    }
    ~~~

5. **Use the package in your application**:

   Now you can create, update, and publish drafts.

    ~~~php
    

    // Create a new post (initially a draft)
    $post = Post::create(['title' => 'My First Post', 'content' => 'Hello World']);

    // Publish the postha
   
    $post->publish();

    // Update the post, which modifies the existing draft or creates a new one
    $post->update(['content' => 'Updated content']);

    // Get the current draft
    $draft = $post->draft;

    // Publish the draft
    $draft->publish();
    ~~~

## Usage

To enable versioning for a model, implement the `Versionable` interface and use the `HasVersions` trait provided by the
package.

### Example with a `Post` Model

First, update your `Post` model:

~~~php


namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use VildanBina\LaravelVersions\Contracts\Versionable;
use VildanBina\LaravelVersions\Concerns\HasVersions;

class Post extends Model implements Versionable
{
    use HasVersions;

    // Optionally, define excluded columns
    protected array $excludedColumns = ['created_at', 'updated_at'];


// Create a new post (initially a draft)
$post = Post::create(['title' => 'Initial Title', 'content' => 'Initial Content']);

// Publish the post
$post->publish();
~~~

#### Updating a Model and Working with Drafts

~~~php


// Update the post, which modifies the existing draft or creates a new one
$post->update(['title' => 'Updated Title']);

// Get the current draft
$draft = $post->draft;

// Check if the post is a draft
if ($draft->isDraft()) {
    echo "This post is currently a draft!";


// Get all versions (drafts and published)
$revisions = $post->revisions;

// Loop through revisions
foreach ($revisions as $revision) {
    echo $revision->title . ' - ' . ($revision->is_published ? 'Published' : 'Draft');