nzesalem / lastus
一个简单易用的包,用于在Laravel模型中添加和管理状态
Requires
- php: >=8.1.0
- doctrine/dbal: ^3.6
- illuminate/support: ^10.7
Requires (Dev)
- mockery/mockery: ^1.5.1
- orchestra/testbench: ^8.3
- phpunit/phpunit: ^10.0
README
为您的Laravel Eloquent模型提供简单的状态添加和管理。
lastus/status是什么?
假设您正在构建一个论坛应用,您通常会有一个User
模型/类。一个用户可以在不同的状态下或在不同的时间点有不同的状态。例如,当用户首次注册论坛时,您可能希望他的/她的状态为未验证
,表示用户尚未验证他的/她的电子邮件。当用户验证他的/她的电子邮件时,他/她可能变为活动
。如果用户违反了论坛的规则之一或多个,他/她可能变为暂停
,依此类推。
Lastus包为Laravel 5设计,旨在自动处理所有这些,配置最小。
-
通过Composer安装包
$ composer require nzesalem/lastus
该包将自动在Laravel 5.5及更高版本中注册自己。
数据库迁移
您的数据库表应该定义一个名为status
的列,类型为tinyInteger
。当您在项目中添加Lastus
后运行php artisan migrate
命令时,Lastus
会自动将此列添加到您的users
表中(如果尚不存在)。
您应该根据需要手动生成其他表的迁移。以下是如何将status
列添加到posts
表的示例。
首先运行
$ php artisan make:migration add_status_field_to_posts_table --table=posts
然后编辑生成的迁移文件(通常位于database/migrations
)
use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class AddStatusFieldToPostsTable extends Migration { public function up() { Schema::table('posts', function (Blueprint $table) { //... $table->tinyInteger('status')->default(0); }); //... } }
最后运行
$ php artisan migrate
更新您的Eloquent模型
您的模型应使用LastusTrait
并定义一个STATUSES
数组常量。
STATUSES
数组的键应全部大写,多个单词应使用下划线分隔。理想情况下,它应遵循与PHP常量相同的命名规则和约定。
STATUSES
数组的值可以是TINYINT
范围内的任何数字。
use Illuminate\Foundation\Auth\User as Authenticatable; use Nzesalem\Lastus\Traits\LastusTrait; class User extends Authenticatable { use LastusTrait; // Key value pair of the statuses you want your model to have const STATUSES = [ 'UNVERIFIED' => 0, 'ACTIVE' => 1, 'SUSPENDED' => 2, 'BLOCKED' => 3, 'PENDING_APPROVAL' => 7, ]; //... }
就这样...
用法
在保存模型时,只需将其状态属性设置为上面定义的一个键,但全部小写,多个单词应使用连字符分隔。例如PENDING_APPROVAL
变为pending-approval
。这是您在处理状态时将使用的格式。
$user = User::create([ 'name' => 'Salem Nzeukwu', 'email' => 'email@domain.com', 'password' => bcrypt('secret'), 'status' => 'active', // This will be saved as '1' in the database ]);
检索模型状态
echo $user->status; // This prints 'active' // Sometime later $user->status = 'pending-approval'; $user->save(); echo $user->status; // This now prints 'pending-approval'
您可以在需要时获取状态代码。例如,状态键字符串在尝试使用它们进行原始查询时将不起作用。因此,在这些情况下,您需要状态代码。
$now = Carbon::now(); // Raw insert query DB::table('users')->insert([ 'name' => 'Firstname Lastname', 'email' => 'fake@example.com', 'password' => bcrypt('secret'), 'created_at' => $now, 'updated_at' => $now, // Get the status code. 'status' => User::getStatusCode('suspended'), ]); // Raw select query $user = User::whereRaw('status = ' . User::getStatusCode('suspended'))->first(); $user->status == 'suspended' // true
获取给定模型的所有定义的状态也很简单,如下面的片段所示。我们获取User
模型的所有定义的状态,并在选择元素中显示它们
<select id="status" class="form-control" name="status" required> <option>Select a status</option> @foreach (App\User::statuses() as $status) <option value="{{ $status }}">{{ ucfirst($status) }}</option> @endforeach </select>
或者,您可以使用Lastus
外观
print_r(Lastus::statuses(App\User::class));
Blade模板
您可以使用@status()
blade指令根据当前登录用户的状态来控制元素的可见性
@status('active') <p>This is only visible to users with an 'active' status.</p> @endstatus
中间件
您可以使用中间件根据状态过滤路由和路由组
Route::group(['prefix' => 'dashboard', 'middleware' => ['status:active']], function() { Route::get('/', 'DashboardController@index'); });