ลองจินตนาการว่าคุณต้องการให้โมเดล 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 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 false ขอบเขต currentStatus จะส่งคืนโมเดลที่มีสถานะตามชื่อที่กำหนด
$ 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 ;
}
} คุณสามารถเปลี่ยนโมเดลที่ใช้ได้โดยการระบุชื่อคลาสในคีย์ status_model ของไฟล์กำหนด model-status
คุณสามารถเปลี่ยนชื่อคอลัมน์ที่ใช้ในตารางสถานะ (โดยค่าเริ่มต้น model_id ) เมื่อใช้การย้ายข้อมูลแบบกำหนดเองที่คุณเปลี่ยนแปลง ในกรณีดังกล่าว เพียงเปลี่ยนคีย์ model_primary_key_attribute ของไฟล์กำหนด model-status
แพคเกจนี้ประกอบด้วยการทดสอบบูรณาการที่ขับเคลื่อนโดยวงออเคสตรา/ม้านั่งทดสอบ
คุณสามารถทำการทดสอบทั้งหมดด้วย:
composer testโปรดดู CHANGELOG สำหรับข้อมูลเพิ่มเติมว่ามีอะไรเปลี่ยนแปลงเมื่อเร็วๆ นี้
โปรดดูการมีส่วนร่วมเพื่อดูรายละเอียด
หากคุณพบข้อบกพร่องเกี่ยวกับการรักษาความปลอดภัย โปรดส่งอีเมลไปที่ [email protected] แทนการใช้ตัวติดตามปัญหา
ใบอนุญาตเอ็มไอที (MIT) โปรดดูไฟล์ใบอนุญาตสำหรับข้อมูลเพิ่มเติม