PHP code example of nekodev / drafty

1. Go to this page and download the library: Download nekodev/drafty 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/ */

    

nekodev / drafty example snippets


'providers' => [
    ...
    Nekodev\Drafty\DraftyServiceProvider::class,
    ...
],

namespace App;

use Illuminate\Database\Eloquent\Model;
use Nekodev\Drafty\Traits\Draftable;
use Nekodev\Drafty\Interfaces\HasDraftable;

class Post extends Model implements HasDraftable
{
    use Draftable;

    /**
     * Get the post's draft.
     */
    public function draft()
    {
        return $this->morphOne('Nekodev\Drafty\Models\Draft', 'draftable');
    }
}

private function saveDraftPost(Request $request, Post $post)
{
    // Update Model data
    $post->title = $request->title;
    $post->content = $request->content;

    // create or update draft and return it 
    $data = $post->saveAsDraft();

    return $data;
}

php artisan make:draft PostDraft

namespace App\Drafts;

use Nekodev\Drafty\Models\Draft as DraftModel;

class PostDraft extends DraftModel
{

}

namespace App;

use Illuminate\Database\Eloquent\Model;
use Nekodev\Drafty\Traits\Draftable;
use Nekodev\Drafty\Interfaces\HasDraftable;
use App\Category;

class Post extends Model implements HasDraftable
{
    use Draftable;

    public $fillable = [ 'title', 'content' ];

    /**
     * Get the post's draft.
     */
    public function draft()
    {
        return $this->morphOne('App\Drafts\PostDraft', 'draftable');
    }

    /**
     * Return categories from post
     */
    public function categories()
    {
        return $this->morphToMany('App\Category', 'categorable');
    }
}

namespace App\Drafts;

use Nekodev\Drafty\Models\Draft as DraftModel;
use App\Category;

class PostDraft extends DraftModel
{
    /**
     * Return categories from draft
     */
    public function categories()
    {
        return $this->morphToMany('App\Category', 'categorable');
    }
}

namespace App;

use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Nekodev\Drafty\Traits\Draftable;
use Nekodev\Drafty\Interfaces\HasDraftable;
use App\Category;

class Post extends Model implements HasMedia, HasDraftable
{
    use Draftable;
    use InteractsWithMedia;

    public $fillable = [ 'title', 'content' ];

    /**
     * Get the post's draft.
     */
    public function draft()
    {
        return $this->morphOne('App\Drafts\PostDraft', 'draftable');
    }
}

namespace App\Drafts;

use Nekodev\Drafty\Models\Draft as DraftModel;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

class PostDraft extends DraftModel implements HasMedia
{
    use InteractsWithMedia;
}
shell
php artisan migrate