在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):spatie/laravel-model-status开源软件地址(OpenSource Url):https://github.com/spatie/laravel-model-status开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Assign statuses to Eloquent modelsImagine you want to have an Eloquent model hold a status. It's easily solved by just adding a This package provides a // set a status
$model->setStatus('pending', 'needs verification');
// set another status
$model->setStatus('accepted');
// specify a reason
$model->setStatus('rejected', 'My rejection reason');
// get the current status
$model->status(); // returns an instance of \Spatie\ModelStatus\Status
// get the previous status
$latestPendingStatus = $model->latestStatus('pending');
$latestPendingStatus->reason; // returns 'needs verification' Support usWe invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products. We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall. InstallationYou can install the package via composer: composer require spatie/laravel-model-status You must publish the migration with: php artisan vendor:publish --provider="Spatie\ModelStatus\ModelStatusServiceProvider" --tag="migrations" Migrate the php artisan migrate Optionally you can publish the config-file with: php artisan vendor:publish --provider="Spatie\ModelStatus\ModelStatusServiceProvider" --tag="config" This is the contents of the file which will be published at return [
/*
* The class name of the status model that holds all statuses.
*
* The model must be or extend `Spatie\ModelStatus\Status`.
*/
'status_model' => Spatie\ModelStatus\Status::class,
/*
* The name of the column which holds the ID of the model related to the statuses.
*
* You can change this value if you have set a different name in the migration for the statuses table.
*/
'model_primary_key_attribute' => 'model_id',
]; UsageAdd the use Spatie\ModelStatus\HasStatuses;
class YourEloquentModel extends Model
{
use HasStatuses;
} Set a new statusYou can set a new status like this: $model->setStatus('status-name'); A reason for the status change can be passed as a second argument. $model->setStatus('status-name', 'optional reason'); Retrieving statusesYou can get the current status of model: $model->status; // returns a string with the name of the latest status
$model->status(); // returns the latest instance of `Spatie\ModelStatus\Status`
$model->latestStatus(); // equivalent to `$model->status()` You can also get latest status of a given name: $model->latestStatus('pending'); // returns an instance of `Spatie\ModelStatus\Status` that has the name `pending` The following examples will return statusses of type $lastStatus = $model->latestStatus(['status 1', 'status 2']);
// or alternatively...
$lastStatus = $model->latestStatus('status 1', 'status 2'); All associated statuses of a model can be retrieved like this: $allStatuses = $model->statuses; Retrieving models with a given latest stateThe $allPendingModels = Model::currentStatus('pending');
//or array of statuses
$allPendingModels = Model::currentStatus(['pending', 'initiated']);
$allPendingModels = Model::currentStatus('pending', 'initiated'); Retrieving models without a given stateThe $allNonPendingModels = Model::otherCurrentStatus('pending'); You can also provide an array of status names to exclude from the query. $allNonInitiatedOrPendingModels = Model::otherCurrentStatus(['initiated', 'pending']);
// or alternatively...
$allNonInitiatedOrPendingModels = Model::otherCurrentStatus('initiated', 'pending'); Validating a status before setting itYou can add custom validation when setting a status by overwriting the public function isValidStatus(string $name, ?string $reason = null): bool
{
...
if (! $condition) {
return false;
}
return true;
} If You may bypass validation with the $model->forceSetStatus('invalid-status-name'); Check if status has been assignedYou can check if a specific status has been set on the model at any time by using the $model->hasEverHadStatus('status 1'); Delete status from modelYou can delete any given status that has been set on the model at any time by using the Delete single status from model: $model->deleteStatus('status 1'); Delete multiple statuses from model at once: $model->deleteStatus(['status 1', 'status 2']); EventsThe namespace Spatie\ModelStatus\Events;
use Illuminate\Database\Eloquent\Model;
use Spatie\ModelStatus\Status;
class StatusUpdated
{
/** @var \Spatie\ModelStatus\Status|null */
public $oldStatus;
/** @var \Spatie\ModelStatus\Status */
public $newStatus;
/** @var \Illuminate\Database\Eloquent\Model */
public $model;
public function __construct(?Status $oldStatus, Status $newStatus, Model $model)
{
$this->oldStatus = $oldStatus;
$this->newStatus = $newStatus;
$this->model = $model;
}
} Custom model and migrationYou can change the model used by specifying a class name in the You can change the column name used in the status table ( TestingThis package contains integration tests that are powered by orchestral/testbench. You can run all tests with: composer test ChangelogPlease see CHANGELOG for more information what has changed recently. ContributingPlease see CONTRIBUTING for details. SecurityIf you've found a bug regarding security please mail [email protected] instead of using the issue tracker. CreditsLicenseThe MIT License (MIT). Please see License File for more information. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论