Easily add status to your models in Laravel 5.
config/draftable.php
配置增加status
选项,增强扩展性。- Model 增加
drafts
方法,仅查询所有草稿。
1). 修改 composer.json
, 在 repositories
数组内追加如下数据
"repositories": [
...
{
"type": "vcs",
"url": "https://github.com/estgroupe-ext/laravel-draftable.git"
}
]
2). 添加 seriousjelly/laravel-draftable
package
composer require estgroupe-ext/laravel-draftable
3). 修改 config/app.php
, 在 providers
数组内追加如下数据
'providers' => array(
...
'Seriousjelly\Draftable\ServiceProvider',
)
4). 生成 migrations, 使其包含 status
字段
class AddModeratioColumnsToPostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->enum('status', [
config('draftable.status.drafts'),
config('draftable.status.publish')
])->default(config('draftable.status.publish'));
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('posts', function(Blueprint $table)
{
$table->dropColumn('status');
});
}
}
5). 更新 Eloquent Models
use Seriousjelly\Draftable\DraftableTrait as Draftable;
class Post extends Model
{
use Draftable;
}
6). 使用
- 返回已发布的 posts
Post::get();
- 返回包含草稿和已发布的 posts
Post::withDrafts()->get();
- 返回只是草稿的 posts
Post::drafts()->get();
Laravel-Draftable was written by Chris Bratherton and released under the MIT License. See the LICENSE file for details.
Copyright 2015 Chris Bratherton