nekodev/drafty

在 Laravel 中为您的模型创建草稿

v1.0.1 2020-07-28 19:22 UTC

This package is auto-updated.

Last update: 2024-09-29 05:55:44 UTC


README

请访问 nekodev 包网站 的完整文档。(即将推出)

兼容 Spatie/MediaLibrary 包 v8

快速开始

安装

使用 composer 安装包

composer require nekodev/drafty

config/app.php 中添加提供者

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

运行迁移

php artisan migrate

此命令将在您的数据库中创建 drafts 表。此表将管理所有草稿数据。

准备您的模型

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');
    }
}

草稿使用多态关系。您必须实现 HasDraftable 接口并使用 draft() 方法。此方法将返回一个 Draft 类模型。

基本用法

控制器示例

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;
}

当调用 $post->save() 方法时,草稿将自动删除。您必须在调用 save 方法之前调用 $post->applyDraft()

草稿定制

您可以创建自己的 Drafts 模型。使用 php artisan make:draft <draft name>

php artisan make:draft PostDraft

此命令将在 app/Drafts 目录中创建一个 draft 方法。

namespace App\Drafts;

use Nekodev\Drafty\Models\Draft as DraftModel;

class PostDraft extends DraftModel
{

}

示例用法

如果您想添加一些关系

POST 模型

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');
    }
}

您必须从可以拥有草稿的模型中创建多态关系。

与 Spatie Media Livrary 一起使用

POST 模型

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;
}

在从 Post 模型应用 save 方法时,将移除所有来自 Post 模型的媒体集合,并替换为 PostDraft 模型集合。

致谢

alt text @nekodesigner (NekoDev) - Tahar CHIBANE | 全栈开发者

享受吧 !! 😃