แพ็คเกจนี้มีคุณสมบัติที่เพิ่มพฤติกรรมการจัดเรียงให้กับโมเดล Eloquent
ค่าของคอลัมน์ลำดับของเรกคอร์ดใหม่ของแบบจำลองถูกกำหนดโดยค่าสูงสุดของคอลัมน์ลำดับของเรกคอร์ดทั้งหมดของโมเดลนั้น + 1
แพคเกจยังมีขอบเขตแบบสอบถามเพื่อดึงข้อมูลบันทึกทั้งหมดตามลำดับที่ถูกต้อง
Spatie เป็นเอเจนซี่ออกแบบเว็บไซต์ในเมืองแอนต์เวิร์ป ประเทศเบลเยียม คุณจะพบภาพรวมของโครงการโอเพ่นซอร์สทั้งหมดของเราบนเว็บไซต์ของเรา
เรียนรู้วิธีสร้างแพ็คเกจเช่นนี้โดยชมหลักสูตรวิดีโอระดับพรีเมียมของเรา:
เราลงทุนทรัพยากรจำนวนมากเพื่อสร้างแพ็คเกจโอเพ่นซอร์สที่ดีที่สุดในระดับเดียวกัน คุณสามารถสนับสนุนเราได้โดยการซื้อหนึ่งในผลิตภัณฑ์ที่ต้องชำระเงินของเรา
เราขอขอบคุณอย่างยิ่งที่คุณส่งโปสการ์ดจากบ้านเกิดของคุณถึงเรา โดยระบุว่าคุณใช้แพ็คเกจใดของเรา คุณจะพบที่อยู่ของเราในหน้าติดต่อของเรา เราเผยแพร่โปสการ์ดที่ได้รับทั้งหมดบนวอลล์โปสการ์ดเสมือนของเรา
สำหรับ Laravel 6.x หรือ PHP 7.x ให้ใช้เวอร์ชัน 3.x ของแพ็คเกจนี้
แพ็คเกจนี้สามารถติดตั้งผ่าน Composer
composer require spatie/eloquent-sortable
ใน Laravel 5.5 ขึ้นไป ผู้ให้บริการจะได้รับการลงทะเบียนโดยอัตโนมัติ ในเฟรมเวิร์กเวอร์ชันเก่า เพียงเพิ่มผู้ให้บริการในไฟล์ config/app.php :
' providers ' => [
...
Spatie EloquentSortable EloquentSortableServiceProvider::class,
];คุณสามารถเลือกเผยแพร่ไฟล์กำหนดค่าด้วย:
php artisan vendor:publish --tag=eloquent-sortable-config นี่คือเนื้อหาของไฟล์ที่จะเผยแพร่ใน config/eloquent-sortable.php
return [
/*
* The name of the column that will be used to sort models.
*/
' order_column_name ' => ' order_column ' ,
/*
* Define if the models should sort when creating. When true, the package
* will automatically assign the highest order number to a new model
*/
' sort_when_creating ' => true ,
/*
* Define if the timestamps should be ignored when sorting.
* When true, updated_at will not be updated when using setNewOrder
*/
' ignore_timestamps ' => false ,
];หากต้องการเพิ่มลักษณะการทำงานที่สามารถจัดเรียงได้ให้กับโมเดลของคุณ คุณต้อง:
SpatieEloquentSortableSortableSpatieEloquentSortableSortableTraitorder_column use Spatie EloquentSortable Sortable ;
use Spatie EloquentSortable SortableTrait ;
class MyModel extends Model implements Sortable
{
use SortableTrait;
public $ sortable = [
' order_column_name ' => ' order_column ' ,
' sort_when_creating ' => true ,
];
// ...
} หากคุณไม่ได้ตั้งค่า $sortable['order_column_name'] แพ็คเกจจะถือว่าชื่อคอลัมน์คำสั่งซื้อของคุณจะมีชื่อว่า order_column
หากคุณไม่ได้ตั้งค่า $sortable['sort_when_creating'] แพ็คเกจจะกำหนดหมายเลขคำสั่งซื้อสูงสุดให้กับโมเดลใหม่โดยอัตโนมัติ
สมมติว่า db-table สำหรับ MyModel ว่างเปล่า:
$ myModel = new MyModel ();
$ myModel -> save (); // order_column for this record will be set to 1
$ myModel = new MyModel ();
$ myModel -> save (); // order_column for this record will be set to 2
$ myModel = new MyModel ();
$ myModel -> save (); // order_column for this record will be set to 3
//the trait also provides the ordered query scope
$ orderedRecords = MyModel:: ordered ()-> get (); คุณสามารถกำหนดลำดับใหม่สำหรับเรคคอร์ดทั้งหมดได้โดยใช้เมธอด setNewOrder
/**
* the record for model id 3 will have order_column value 1
* the record for model id 1 will have order_column value 2
* the record for model id 2 will have order_column value 3
*/
MyModel:: setNewOrder ([ 3 , 1 , 2 ]);คุณสามารถเลือกส่งหมายเลขลำดับเริ่มต้นเป็นอาร์กิวเมนต์ที่สองได้
/**
* the record for model id 3 will have order_column value 11
* the record for model id 1 will have order_column value 12
* the record for model id 2 will have order_column value 13
*/
MyModel:: setNewOrder ([ 3 , 1 , 2 ], 10 );คุณสามารถแก้ไขแบบสอบถามที่จะดำเนินการโดยส่งการปิดเป็นอาร์กิวเมนต์ที่สี่
/**
* the record for model id 3 will have order_column value 11
* the record for model id 1 will have order_column value 12
* the record for model id 2 will have order_column value 13
*/
MyModel:: setNewOrder ([ 3 , 1 , 2 ], 10 , null , function ( $ query ) {
$ query -> withoutGlobalScope ( new ActiveScope );
}); หากต้องการเรียงลำดับโดยใช้คอลัมน์อื่นที่ไม่ใช่คีย์หลัก ให้ใช้เมธอด setNewOrderByCustomColumn
/**
* the record for model uuid '7a051131-d387-4276-bfda-e7c376099715' will have order_column value 1
* the record for model uuid '40324562-c7ca-4c69-8018-aff81bff8c95' will have order_column value 2
* the record for model uuid '5dc4d0f4-0c88-43a4-b293-7c7902a3cfd1' will have order_column value 3
*/
MyModel:: setNewOrderByCustomColumn ( ' uuid ' , [
' 7a051131-d387-4276-bfda-e7c376099715 ' ,
' 40324562-c7ca-4c69-8018-aff81bff8c95 ' ,
' 5dc4d0f4-0c88-43a4-b293-7c7902a3cfd1 '
]); เช่นเดียวกับ setNewOrder setNewOrderByCustomColumn จะยอมรับอาร์กิวเมนต์ลำดับเริ่มต้นที่เป็นตัวเลือกด้วย
/**
* the record for model uuid '7a051131-d387-4276-bfda-e7c376099715' will have order_column value 10
* the record for model uuid '40324562-c7ca-4c69-8018-aff81bff8c95' will have order_column value 11
* the record for model uuid '5dc4d0f4-0c88-43a4-b293-7c7902a3cfd1' will have order_column value 12
*/
MyModel:: setNewOrderByCustomColumn ( ' uuid ' , [
' 7a051131-d387-4276-bfda-e7c376099715 ' ,
' 40324562-c7ca-4c69-8018-aff81bff8c95 ' ,
' 5dc4d0f4-0c88-43a4-b293-7c7902a3cfd1 '
], 10 );คุณยังสามารถย้ายโมเดลขึ้นหรือลงได้ด้วยวิธีเหล่านี้:
$ myModel -> moveOrderDown ();
$ myModel -> moveOrderUp ();คุณยังสามารถย้ายแบบจำลองไปยังตำแหน่งแรกหรือตำแหน่งสุดท้ายได้:
$ myModel -> moveToStart ();
$ myModel -> moveToEnd ();คุณสามารถกำหนดได้ว่าองค์ประกอบใดเป็นลำดับแรกหรือลำดับสุดท้าย:
$ myModel -> isFirstInOrder ();
$ myModel -> isLastInOrder ();คุณสามารถสลับลำดับของสองรุ่นได้:
MyModel:: swapOrder ( $ myModel , $ anotherModel ); หากโมเดล/ตารางของคุณมีฟิลด์การจัดกลุ่ม (โดยปกติจะเป็นคีย์นอก): id, user_id , title, order_column และคุณต้องการให้วิธีการข้างต้นนำมาพิจารณา คุณสามารถสร้างเมธอด buildSortQuery ที่โมเดลของคุณได้:
// MyModel.php
public function buildSortQuery ()
{
return static :: query ()-> where ( ' user_id ' , $ this -> user_id );
}ซึ่งจะจำกัดการคำนวณเฉพาะค่าฟิลด์ของอินสแตนซ์แบบจำลอง
เมื่อการเรียงลำดับเสร็จสิ้น กิจกรรม ( SpatieEloquentSortableEloquentModelSortedEvent ) จะถูกส่งออกไปซึ่งคุณสามารถรับฟังได้ สิ่งนี้มีประโยชน์สำหรับการรันตรรกะหลังการเรียงลำดับ เช่น การล้างแคชหรือการดำเนินการอื่นๆ ที่จำเป็นต้องดำเนินการหลังจากการเรียงลำดับ
กิจกรรมนี้มีตัวช่วย isFor ซึ่งช่วยให้คุณตรวจสอบคลาส Eloquent ที่ถูกจัดเรียงได้อย่างสะดวก
ด้านล่างนี้คือตัวอย่างวิธีที่คุณสามารถรับฟังกิจกรรมนี้:
use Spatie EloquentSortable EloquentModelSortedEvent as SortEvent ;
class SortingListener
{
public function handle ( SortEvent $ event ): void {
if ( $ event -> isFor (MyClass::class)) {
// ToDo: flush our cache
}
}
}แพคเกจประกอบด้วยการทดสอบการรวม/ควัน ตั้งค่ากับ Orchestra การทดสอบสามารถทำได้ผ่าน phpunit
vendor/bin/phpunitโปรดดู CHANGELOG สำหรับข้อมูลเพิ่มเติมเกี่ยวกับสิ่งที่เปลี่ยนแปลงเมื่อเร็วๆ นี้
โปรดดูการมีส่วนร่วมเพื่อดูรายละเอียด
โปรดตรวจสอบนโยบายความปลอดภัยของเราเกี่ยวกับวิธีการรายงานจุดอ่อนด้านความปลอดภัย
ใบอนุญาตเอ็มไอที (MIT) โปรดดูไฟล์ใบอนุญาตสำหรับข้อมูลเพิ่มเติม