想像一下,您想讓一個 Eloquent 模型保持一定的地位。只需向該模型添加一個status欄位並使用它即可輕鬆解決此問題。但是,如果您需要狀態變更的歷史記錄或需要儲存一些有關狀態變更原因的額外信息,僅新增單一欄位是不夠的。
該套件提供了HasStatuses特徵,一旦安裝在模型上,您就可以執行以下操作:
// 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 SpatieModelStatusStatus
// get the previous status
$ latestPendingStatus = $ model -> latestStatus ( ' pending ' );
$ latestPendingStatus -> reason ; // returns 'needs verification' 我們投入了大量資源來創建一流的開源套件。您可以透過購買我們的一款付費產品來支持我們。
我們非常感謝您從家鄉寄給我們一張明信片,並註明您正在使用我們的哪種套餐。您可以在我們的聯絡頁面上找到我們的地址。我們在虛擬明信片牆上發布所有收到的明信片。
您可以透過 Composer 安裝該軟體包:
composer require spatie/laravel-model-status您必須透過以下方式發布遷移:
php artisan vendor:publish --provider= " SpatieModelStatusModelStatusServiceProvider " --tag= " migrations "遷移statuses表:
php artisan migrate您可以選擇使用以下方式發佈設定檔:
php artisan vendor:publish --provider= " SpatieModelStatusModelStatusServiceProvider " --tag= " config "這是將在config/model-status.php發布的文件的內容
return [
/*
* The class name of the status model that holds all statuses.
*
* The model must be or extend `SpatieModelStatus 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 ' ,
];將HasStatuses特徵加入到您想要使用狀態的模型中。
use Spatie ModelStatus HasStatuses ;
class YourEloquentModel extends Model
{
use HasStatuses;
}您可以像這樣設定新狀態:
$ model -> setStatus ( ' status-name ' );狀態變更的原因可以作為第二個參數傳遞。
$ model -> setStatus ( ' status-name ' , ' optional reason ' );您可以取得模型的當前狀態:
$ model -> status ; // returns a string with the name of the latest status
$ model -> status (); // returns the latest instance of `SpatieModelStatusStatus`
$ model -> latestStatus (); // equivalent to `$model->status()`您也可以獲得給定名稱的最新狀態:
$ model -> latestStatus ( ' pending ' ); // returns an instance of `SpatieModelStatusStatus` that has the name `pending`取得模型的所有可用狀態名稱。
$ statusNames = $ model -> getStatusNames (); // returns a collection of all available status names.以下範例將傳回類型為status 1或status 2的狀態(以最新者為準)。
$ lastStatus = $ model -> latestStatus ([ ' status 1 ' , ' status 2 ' ]);
// or alternatively...
$ lastStatus = $ model -> latestStatus ( ' status 1 ' , ' status 2 ' );可以像這樣檢索模型的所有關聯狀態:
$ allStatuses = $ model -> statuses ;這將檢查模型是否具有狀態:
$ model -> setStatus ( ' status1 ' );
$ isStatusExist = $ model -> hasStatus ( ' status1 ' ); // return true
$ isStatusExist = $ model -> hasStatus ( ' status2 ' ); // return falsecurrentStatus範圍將傳回具有給定名稱狀態的模型。
$ allPendingModels = Model:: currentStatus ( ' pending ' );
//or array of statuses
$ allPendingModels = Model:: currentStatus ([ ' pending ' , ' initiated ' ]);
$ allPendingModels = Model:: currentStatus ( ' pending ' , ' initiated ' );otherCurrentStatus作用域將傳回不具有給定名稱狀態的所有模型,包括不具有與其關聯的任何狀態的任何模型。
$ allNonPendingModels = Model:: otherCurrentStatus ( ' pending ' );您也可以提供要從查詢中排除的狀態名稱陣列。
$ allNonInitiatedOrPendingModels = Model:: otherCurrentStatus ([ ' initiated ' , ' pending ' ]);
// or alternatively...
$ allNonInitiatedOrPendingModels = Model:: otherCurrentStatus ( ' initiated ' , ' pending ' );您可以在設定狀態時透過覆寫isValidStatus方法新增自訂驗證:
public function isValidStatus ( string $ name , ? string $ reason = null ): bool
{
. . .
if (! $ condition ) {
return false ;
}
return true ;
}如果isValidStatus回傳false則會拋出SpatieModelStatusExceptionsInvalidStatus例外。
您可以使用forceSetStatus方法繞過驗證:
$ model -> forceSetStatus ( ' invalid-status-name ' );您可以隨時使用hasEverHadStatus方法檢查模型上是否已設定特定狀態:
$ model -> hasEverHadStatus ( ' status 1 ' );您可以使用hasNeverHadStatus方法隨時檢查模型上是否從未設定過特定狀態:
$ model -> hasNeverHadStatus ( ' status 1 ' );您可以隨時使用deleteStatus方法刪除模型上設定的任何給定狀態:
從模型中刪除單一狀態:
$ model -> deleteStatus ( ' status 1 ' );一次從模型中刪除多個狀態:
$ model -> deleteStatus ([ ' status 1 ' , ' status 2 ' ]);當狀態更新時,將調度SpatieModelStatusEventsStatusUpdated事件。
namespace Spatie ModelStatus Events ;
use Illuminate Database Eloquent Model ;
use Spatie ModelStatus Status ;
class StatusUpdated
{
/** @var SpatieModelStatus Status | null */
public $ oldStatus ;
/ ** @var SpatieModelStatus Status */
public $ newStatus ;
/ ** @var IlluminateDatabaseEloquent Model */
public $ model ;
public function __construct (? Status $ oldStatus , Status $ newStatus , Model $ model )
{
$ this -> oldStatus = $ oldStatus ;
$ this -> newStatus = $ newStatus ;
$ this -> model = $ model ;
}
}您可以透過在model-status設定檔的status_model鍵中指定類別名稱來變更所使用的模型。
當使用自訂遷移時,您可以變更狀態表中使用的欄位名稱(預設為model_id )。在這種情況下,只需更改model-status設定檔的model_primary_key_attribute鍵即可。
該軟體包包含由 Orchestral/testbench 提供支援的整合測試。
您可以使用以下命令執行所有測試:
composer test請參閱變更日誌以了解最近變更的更多資訊。
詳細資訊請參閱貢獻。
如果您發現有關安全的錯誤,請發送郵件至 [email protected],而不是使用問題追蹤器。
麻省理工學院許可證 (MIT)。請參閱許可證文件以獲取更多資訊。