พื้นที่เก็บข้อมูลนี้มีมาโครคอลเลกชันที่มีประโยชน์บางส่วน
Spatie เป็นเอเจนซี่ออกแบบเว็บไซต์ที่ตั้งอยู่ในเมืองแอนต์เวิร์ป ประเทศเบลเยียม คุณจะพบภาพรวมของโครงการโอเพ่นซอร์สทั้งหมดของเราบนเว็บไซต์ของเรา
เราลงทุนทรัพยากรจำนวนมากเพื่อสร้างแพ็คเกจโอเพ่นซอร์สที่ดีที่สุดในระดับเดียวกัน คุณสามารถสนับสนุนเราได้โดยการซื้อหนึ่งในผลิตภัณฑ์ที่ต้องชำระเงินของเรา
เราขอขอบคุณอย่างยิ่งที่คุณส่งโปสการ์ดจากบ้านเกิดของคุณถึงเรา โดยระบุว่าคุณใช้แพ็คเกจใดของเรา คุณจะพบที่อยู่ของเราในหน้าติดต่อของเรา เราเผยแพร่โปสการ์ดที่ได้รับทั้งหมดบนวอลล์โปสการ์ดเสมือนของเรา
คุณสามารถดึงแพ็คเกจผ่านผู้แต่ง:
composer require spatie/laravel-collection-macros
แพ็คเกจจะลงทะเบียนเองโดยอัตโนมัติ
after
at
second
third
fourth
fifth
sixth
seventh
eighth
ninth
tenth
getNth
before
catch
chunkBy
collectBy
containsAny
containsAll
eachCons
extract
filterMap
firstOrFail
firstOrPush
fromPairs
getCaseInsensitive
glob
groupByModel
hasCaseInsensitive
head
if
ifAny
ifEmpty
insertAfter
insertAfterKey
insertAt
insertBefore
insertBeforeKey
none
paginate
path
pluckMany
pluckManyValues
pluckToArray
prioritize
recursive
rotate
sectionBy
simplePaginate
sliceBefore
tail
try
toPairs
transpose
validate
weightedRandom
withSize
after
รับรายการถัดไปจากคอลเลกชัน
$ collection = collect ([ 1 , 2 , 3 ]);
$ currentItem = 2 ;
$ currentItem = $ collection -> after ( $ currentItem ); // return 3;
$ collection -> after ( $ currentItem ); // return null;
$ currentItem = $ collection -> after ( function ( $ item ) {
return $ item > 1 ;
}); // return 3;
คุณยังสามารถส่งพารามิเตอร์ตัวที่สองเพื่อใช้เป็นทางเลือกได้อีกด้วย
$ collection = collect ([ 1 , 2 , 3 ]);
$ currentItem = 3 ;
$ collection -> after ( $ currentItem , $ collection -> first ()); // return 1;
at
ดึงรายการที่ดัชนี
$ data = new Collection ([ 1 , 2 , 3 ]);
$ data -> at ( 0 ); // 1
$ data -> at ( 1 ); // 2
$ data -> at (- 1 ); // 3
second
ดึงรายการที่ดัชนีที่สอง
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> second (); // 2
third
ดึงรายการที่ดัชนีที่สาม
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> third (); // 3
fourth
ดึงรายการที่ดัชนีที่สี่
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> fourth (); // 4
fifth
ดึงรายการที่ดัชนีที่ห้า
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> fifth (); // 5
sixth
ดึงรายการที่ดัชนีที่หก
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> sixth (); // 6
seventh
ดึงรายการที่ดัชนีที่เจ็ด
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> seventh (); // 7
eighth
ดึงรายการที่ดัชนีที่แปด
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> eighth (); // 8
ninth
ดึงรายการที่ดัชนีที่เก้า
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> ninth (); // 9
tenth
ดึงรายการที่ดัชนีที่สิบ
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> tenth (); // 10
getNth
รับไอเท็มที่ไอเท็มที่ n
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ]);
$ data -> getNth ( 11 ); // 11
before
รับรายการก่อนหน้าจากคอลเลกชัน
$ collection = collect ([ 1 , 2 , 3 ]);
$ currentItem = 2 ;
$ currentItem = $ collection -> before ( $ currentItem ); // return 1;
$ collection -> before ( $ currentItem ); // return null;
$ currentItem = $ collection -> before ( function ( $ item ) {
return $ item > 2 ;
}); // return 2;
คุณยังสามารถส่งพารามิเตอร์ตัวที่สองเพื่อใช้เป็นทางเลือกได้อีกด้วย
$ collection = collect ([ 1 , 2 , 3 ]);
$ currentItem = 1 ;
$ collection -> before ( $ currentItem , $ collection -> last ()); // return 3;
catch
ดู Try
chunkBy
แบ่งค่าจากคอลเลกชันออกเป็นกลุ่มตราบใดที่การเรียกกลับที่กำหนดเป็นจริง หากพารามิเตอร์ทางเลือก $preserveKeys
เป็น true
ถูกส่งผ่าน พารามิเตอร์นั้นจะคงคีย์ดั้งเดิมไว้
collect ([ ' A ' , ' A ' , ' B ' , ' A ' ])-> chunkBy ( function ( $ item ) {
return $ item == ' A ' ;
}); // return Collection([['A', 'A'],['B'], ['A']])
collectBy
รับไอเทมตามรหัสที่กำหนดและรวบรวมมัน
$ collection = collect ([
' foo ' => [ 1 , 2 , 3 ],
' bar ' => [ 4 , 5 , 6 ],
]);
$ collection -> collectBy ( ' foo ' ); // Collection([1, 2, 3])
คุณยังสามารถส่งพารามิเตอร์ตัวที่สองเพื่อใช้เป็นทางเลือกได้อีกด้วย
$ collection = collect ([
' foo ' => [ 1 , 2 , 3 ],
' bar ' => [ 4 , 5 , 6 ],
]);
$ collection -> collectBy ( ' baz ' , [ ' Nope ' ]); // Collection(['Nope'])
containsAny
จะส่งกลับ true
หากมีค่าที่กำหนดตั้งแต่หนึ่งค่าขึ้นไปในคอลเลกชัน
$ collection = collect ([ ' a ' , ' b ' , ' c ' ]);
$ collection -> containsAny ([ ' b ' , ' c ' , ' d ' ]); // returns true
$ collection -> containsAny ([ ' c ' , ' d ' , ' e ' ]); // returns true
$ collection -> containsAny ([ ' d ' , ' e ' , ' f ' ]); // returns false
$ collection -> containsAny ([]); // returns false
containsAll
จะส่งคืน true
หากค่าที่กำหนดทั้งหมดมีอยู่ในคอลเลกชัน
$ collection = collect ([ ' a ' , ' b ' , ' c ' ]);
$ collection -> containsAll ([ ' b ' , ' c ' ,]); // returns true
$ collection -> containsAll ([ ' c ' , ' d ' ]); // returns false
$ collection -> containsAll ([ ' d ' , ' e ' ]); // returns false
$ collection -> containsAll ([]); // returns true
eachCons
รับเพื่อนบ้านที่ต่อเนื่องกันต่อไปนี้ในคอลเลกชันจากขนาดก้อนที่กำหนด หากพารามิเตอร์ทางเลือก $preserveKeys
เป็น true
ถูกส่งผ่าน พารามิเตอร์นั้นจะคงคีย์ดั้งเดิมไว้
collect ([ 1 , 2 , 3 , 4 ])-> eachCons ( 2 ); // return collect([[1, 2], [2, 3], [3, 4]])
extract
แยกคีย์ออกจากคอลเลกชัน สิ่งนี้คล้ายกันมากกับ only
โดยมีข้อแตกต่างที่สำคัญสองประการ:
extract
ส่งคืนอาร์เรย์ของค่า ไม่ใช่อาร์เรย์ที่เชื่อมโยงnull
แทนที่จะละเว้น extract
มีประโยชน์เมื่อใช้ไวยากรณ์ list()
ของ PHP 7.1
[ $ name , $ role ] = collect ( $ user )-> extract ( ' name ' , ' role.name ' );
filterMap
แมปคอลเลกชันและลบค่าเท็จในครั้งเดียว
$ collection = collect ([ 1 , 2 , 3 , 4 , 5 , 6 ])-> filterMap ( function ( $ number ) {
$ quotient = $ number / 3 ;
return is_integer ( $ quotient ) ? $ quotient : null ;
});
$ collection -> toArray (); // returns [1, 2]
firstOrFail
รับสินค้าชิ้นแรก. พ่น SpatieCollectionMacrosExceptionsCollectionItemNotFound
หากไม่พบรายการ
$ collection = collect ([ 1 , 2 , 3 , 4 , 5 , 6 ])-> firstOrFail ();
$ collection -> toArray (); // returns [1]
collect ([])-> firstOrFail (); // throws SpatieCollectionMacrosExceptionsCollectionItemNotFound
firstOrPush
ดึงข้อมูลรายการแรกโดยใช้ callable ที่กำหนดเป็นพารามิเตอร์แรก หากไม่มีค่า ให้พุชค่าของพารามิเตอร์ตัวที่สองลงในคอลเลกชัน คุณสามารถส่งผ่าน callable เป็นพารามิเตอร์ตัวที่สองได้
วิธีการนี้มีประโยชน์มากเมื่อต้องจัดการกับคุณสมบัติของคลาสที่แคชไว้ ซึ่งคุณต้องการจัดเก็บค่าที่ดึงมาจาก API หรือฟังก์ชันที่มีราคาแพงในการคำนวณในคอลเลกชันเพื่อใช้หลายครั้ง
$ collection = collect ([ 1 , 2 , 3 ])-> firstOrPush ( fn ( $ item ) => $ item === 4 , 4 );
$ collection -> toArray (); // returns [1, 2, 3, 4]
ในบางครั้ง คุณจะต้องระบุคอลเลกชันเป้าหมายที่จะพุชไป คุณสามารถส่งผ่านนี้เป็นพารามิเตอร์ที่สาม
$ collection = collect ([ 1 , 2 , 3 ]);
$ collection -> filter ()-> firstOrPush ( fn ( $ item ) => $ item === 4 , 4 , $ collection );
$ collection -> toArray (); // returns [1, 2, 3, 4]
fromPairs
แปลงคอลเลกชันเป็นรายการคอลเลกชันแบบฟอร์มอาเรย์ที่เชื่อมโยง
$ collection = collect ([[ ' a ' , ' b ' ], [ ' c ' , ' d ' ], [ ' e ' , ' f ' ]])-> fromPairs ();
$ collection -> toArray (); // returns ['a' => 'b', 'c' => 'd', 'e' => 'f']
getCaseInsensitive
รับค่าของคีย์ที่กำหนด
หากคีย์เป็นสตริง เราจะค้นหาคีย์โดยใช้การเปรียบเทียบที่ไม่คำนึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่
$ collection = collect ([
' foo ' => ' bar ' ,
]);
$ collection -> getCaseInsensitive ( ' Foo ' ); // returns 'bar';
glob
ส่งกลับชุดของผลลัพธ์ glob()
Collection:: glob ( ' config/*.php ' );
groupByModel
คล้ายกับ groupBy
แต่จัดกลุ่มคอลเลกชันตามโมเดล Eloquent เนื่องจากคีย์เป็นอ็อบเจ็กต์แทนที่จะเป็นจำนวนเต็มหรือสตริง ผลลัพธ์จึงถูกแบ่งออกเป็นอาร์เรย์ที่แยกจากกัน
$ posts -> groupByModel ( ' category ' );
// [
// [$categoryA, [/*...$posts*/]],
// [$categoryB, [/*...$posts*/]],
// ];
ลายเซ็นแบบเต็ม: groupByModel($callback, $preserveKeys, $modelKey, $itemsKey)
hasCaseInsensitive
ตรวจสอบว่าคอลเลกชันมีคีย์ที่มีชื่อที่กำหนดหรือไม่
ถ้า $key เป็นสตริง เราจะค้นหาคีย์โดยใช้การเปรียบเทียบที่ไม่คำนึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่
$ collection = collect ([
' foo ' => ' bar ' ,
]);
$ collection -> hasCaseInsensitive ( ' Foo ' ); // returns true;
head
ดึงข้อมูลรายการแรกจากคอลเลกชัน
$ collection = collect ([ 1 , 2 , 3 ]);
$ collection -> head (); // return 1
$ collection = collect ([]);
$ collection -> head (); // return null
if
แมโคร if
สามารถช่วยเชนการรวบรวมสาขาได้ นี่คือลายเซ็นของมาโครนี้:
if (mixed $ if , mixed $ then = null , mixed $ else = null ): mixed
$if
, $then
และ $else
สามารถเป็นประเภทใดก็ได้ หากมีการส่งผ่านการปิดไปยังพารามิเตอร์ใดๆ เหล่านี้ การปิดนั้นจะถูกดำเนินการและมาโครจะใช้ผลลัพธ์ของมัน
เมื่อ $if
ส่งกลับค่าความจริง $then
จะถูกส่งคืน มิฉะนั้น $else
จะถูกส่งคืน
นี่คือตัวอย่างบางส่วน:
collect ()-> if ( true , then: true , else: false ); // returns true
collect ()-> if ( false , then: true , else: false ); // returns false
เมื่อการปิดถูกส่งไปที่ $if
, $then
หรือ $else
คอลเลกชันทั้งหมดจะถูกส่งผ่านเป็นอาร์กิวเมนต์ของการปิดนั้น
// the `then` closure will be executed
// the first element of the returned collection now contains "THIS IS THE VALUE"
$ collection = collect ([ ' this is a value ' ])
-> if (
fn ( Collection $ collection ) => $ collection -> contains ( ' this is a value ' ),
then: fn ( Collection $ collection ) => $ collection -> map ( fn ( string $ item ) => strtoupper ( $ item )),
else: fn ( Collection $ collection ) => $ collection -> map ( fn ( string $ item ) => Str:: kebab ( $ item ))
);
// the `else` closure will be executed
// the first element of the returned collection now contains "this-is-another-value"
$ collection = collect ([ ' this is another value ' ])
-> if (
fn ( Collection $ collection ) => $ collection -> contains ( ' this is a value ' ),
then: fn ( Collection $ collection ) => $ collection -> map ( fn ( string $ item ) => strtoupper ( $ item )),
else: fn ( Collection $ collection ) => $ collection -> map ( fn ( string $ item ) => Str:: kebab ( $ item ))
);
ifAny
ดำเนินการ callable ที่ส่งผ่านหากคอลเลกชันไม่ว่างเปล่า คอลเลกชันทั้งหมดจะถูกส่งคืน
collect ()-> ifAny ( function ( Collection $ collection ) { // empty collection so this won't get called
echo ' Hello ' ;
});
collect ([ 1 , 2 , 3 ])-> ifAny ( function ( Collection $ collection ) { // non-empty collection so this will get called
echo ' Hello ' ;
});
ifEmpty
ดำเนินการ callable ที่ส่งผ่านหากคอลเลกชันว่างเปล่า คอลเลกชันทั้งหมดจะถูกส่งคืน
collect ()-> ifEmpty ( function ( Collection $ collection ) { // empty collection so this will called
echo ' Hello ' ;
});
collect ([ 1 , 2 , 3 ])-> ifEmpty ( function ( Collection $ collection ) { // non-empty collection so this won't get called
echo ' Hello ' ;
});
insertAfter
แทรกรายการหลังจากการปรากฏครั้งแรกของรายการที่กำหนด และส่งคืนอินสแตนซ์คอลเลกชันที่อัปเดต สามารถเลือกให้กุญแจได้
collect ([ ' zero ' , ' two ' , ' three ' ])-> insertAfter ( ' zero ' , ' one ' );
// Collection contains ['zero', 'one', 'two', 'three']
collect ([ ' zero ' => 0 , ' two ' => 2 , ' three ' => 3 ]-> insertAfter ( 0 , 5 , ' five ' );
// Collection contains ['zero' => 0, 'five' => 5, 'two' => 2, 'three' => 3]
insertAfterKey
แทรกรายการหลังคีย์ที่กำหนดและส่งคืนอินสแตนซ์คอลเลกชันที่อัปเดต สามารถเลือกให้คีย์สำหรับรายการใหม่ได้
collect ([ ' zero ' , ' two ' , ' three ' ])-> insertAfterKey ( 0 , ' one ' );
// Collection contains ['zero', 'one', 'two', 'three']
collect ([ ' zero ' => 0 , ' two ' => 2 , ' three ' => 3 ]-> insertAfterKey ( ' zero ' , 5 , ' five ' );
// Collection contains ['zero' => 0, 'five' => 5, 'two' => 2, 'three' => 3]
insertAt
แทรกรายการในดัชนีที่กำหนดและส่งคืนอินสแตนซ์คอลเลกชันที่อัปเดต สามารถเลือกให้กุญแจได้
collect ([ ' zero ' , ' two ' , ' three ' ])-> insertAt ( 1 , ' one ' );
// Collection contains ['zero', 'one', 'two', 'three']
collect ([ ' zero ' => 0 , ' two ' => 2 , ' three ' => 3 ]-> insertAt ( 1 , 5 , ' five ' );
// Collection contains ['zero' => 0, 'five' => 5, 'two' => 2, 'three' => 3]
insertBefore
แทรกรายการก่อนการปรากฏครั้งแรกของรายการที่กำหนด และส่งคืนอินสแตนซ์คอลเลกชันที่อัปเดต สามารถเลือกให้กุญแจได้
collect ([ ' zero ' , ' two ' , ' three ' ])-> insertBefore ( ' two ' , ' one ' );
// Collection contains ['zero', 'one', 'two', 'three']
collect ([ ' zero ' => 0 , ' two ' => 2 , ' three ' => 3 ]-> insertBefore ( 2 , 5 , ' five ' );
// Collection contains ['zero' => 0, 'five' => 5, 'two' => 2, 'three' => 3]
insertBeforeKey
แทรกรายการก่อนคีย์ที่กำหนดและส่งคืนอินสแตนซ์คอลเลกชันที่อัปเดต สามารถเลือกให้คีย์สำหรับรายการใหม่ได้
collect ([ ' zero ' , ' two ' , ' three ' ])-> insertBeforeKey ( 1 , ' one ' );
// Collection contains ['zero', 'one', 'two', 'three']
collect ([ ' zero ' => 0 , ' two ' => 2 , ' three ' => 3 ]-> insertBeforeKey ( ' two ' , 5 , ' five ' );
// Collection contains ['zero' => 0, 'five' => 5, 'two' => 2, 'three' => 3]
none
ตรวจสอบว่าคอลเลกชันไม่มีรายการที่กำหนด คู่คีย์-ค่า หรือการทดสอบความจริงที่ผ่านการทดสอบหรือไม่ ฟังก์ชันยอมรับพารามิเตอร์เดียวกันกับวิธีการ contains
collect ([ ' foo ' ])-> none ( ' bar ' ); // returns true
collect ([ ' foo ' ])-> none ( ' foo ' ); // returns false
collect ([[ ' name ' => ' foo ' ]])-> none ( ' name ' , ' bar ' ); // returns true
collect ([[ ' name ' => ' foo ' ]])-> none ( ' name ' , ' foo ' ); // returns false
collect ([ ' name ' => ' foo ' ])-> none ( function ( $ key , $ value ) {
return $ key === ' name ' && $ value === ' bar ' ;
}); // returns true
paginate
สร้างอินสแตนซ์ LengthAwarePaginator
สำหรับรายการในคอลเลกชัน
collect ( $ posts )-> paginate ( 5 );
วิธีนี้จะแบ่งหน้าเนื้อหาของ $posts
ด้วย 5 รายการต่อหน้า paginate
ยอมรับตัวเลือกบางอย่าง ตรงไปที่เอกสาร Laravel เพื่อดูคำแนะนำเชิงลึก
paginate(int $perPage = 15, string $pageName = 'page', ?int $page = null, ?int $total = null, array $options = [])
path
ส่งคืนรายการจากคอลเลกชันที่มีข้อมูลหลายมิติโดยใช้สัญลักษณ์ "จุด" ทำงานในลักษณะเดียวกับวิธี pull
ของคอลเลกชันดั้งเดิม แต่ไม่ต้องลบรายการออกจากคอลเลกชัน
$ collection = new Collection ([
' foo ' => [
' bar ' => [
' baz ' => ' value ' ,
]
]
]);
$ collection -> path ( ' foo.bar.baz ' ) // 'value'
pluckMany
ส่งคืนคอลเลกชันที่มีเฉพาะคีย์ที่ระบุ
$ collection = collect ([
[ ' a ' => 1 , ' b ' => 10 , ' c ' => 100 ],
[ ' a ' => 2 , ' b ' => 20 , ' c ' => 200 ],
]);
$ collection -> pluckMany ([ ' a ' , ' b ' ]);
// returns
// collect([
// ['a' => 1, 'b' => 10],
// ['a' => 2, 'b' => 20],
// ]);
pluckManyValues
ส่งกลับคอลเลกชันที่มีค่าของคีย์ที่ระบุเท่านั้น
$ collection = collect ([
[ ' a ' => 1 , ' b ' => 10 , ' c ' => 100 ],
[ ' a ' => 2 , ' b ' => 20 , ' c ' => 200 ],
]);
$ collection -> pluckMany ([ ' a ' , ' b ' ]);
// returns
// collect([
// [1, 10],
// [2, 20],
// ]);
pluckToArray
ส่งกลับอาร์เรย์ของค่าของคีย์ที่กำหนด
$ collection = collect ([
[ ' a ' => 1 , ' b ' => 10 ],
[ ' a ' => 2 , ' b ' => 20 ],
[ ' a ' => 3 , ' b ' => 30 ]
]);
$ collection -> pluckToArray ( ' a ' ); // returns [1, 2, 3]
prioritize
ย้ายองค์ประกอบไปยังจุดเริ่มต้นของคอลเลกชัน
$ collection = collect ([
[ ' id ' => 1 ],
[ ' id ' => 2 ],
[ ' id ' => 3 ],
]);
$ collection
-> prioritize ( function ( array $ item ) {
return $ item [ ' id ' ] === 2 ;
})
-> pluck ( ' id ' )
-> toArray (); // returns [2, 1, 3]
recursive
แปลงอาร์เรย์และลูกของอาร์เรย์ให้เป็นคอลเลกชันโดยใช้การเรียกซ้ำ
collect ([
' item ' => [
' children ' => []
]
])-> recursive ();
// subsequent arrays are now collections
ในบางกรณี คุณอาจไม่ต้องการเปลี่ยนเด็กๆ ทั้งหมดให้เป็นคอลเลกชั่น คุณสามารถแปลงเป็นความลึกที่แน่นอนได้โดยการใส่ตัวเลขให้กับวิธีการเรียกซ้ำ
collect ([
' item ' => [
' children ' => [
' one ' => [ 1 ],
' two ' => [ 2 ]
]
]
])-> recursive ( 1 ); // Collection(['item' => Collection(['children' => ['one' => [1], 'two' => [2]]])])
สิ่งนี้มีประโยชน์เมื่อคุณรู้ว่าในระดับความลึกหนึ่งมันไม่จำเป็น หรือมันอาจทำให้โค้ดของคุณเสียหาย
collect ([
' item ' => [
' children ' => [
' one ' => [ 1 ],
' two ' => [ 2 ]
]
]
])
-> recursive ( 1 )
-> map ( function ( $ item ) {
return $ item -> map ( function ( $ children ) {
return $ children -> mapInto (Model::class);
});
}); // Collection(['item' => Collection(['children' => ['one' => Model(), 'two' => Model()]])])
// If we do not pass a max depth we will get the error "Argument #1 ($attributes) must be of type array"
rotate
หมุนรายการในคอลเลกชันด้วยการชดเชยที่กำหนด
$ collection = collect ([ 1 , 2 , 3 , 4 , 5 , 6 ]);
$ rotate = $ collection -> rotate ( 1 );
$ rotate -> toArray ();
// [2, 3, 4, 5, 6, 1]
sectionBy
แยกคอลเลกชันออกเป็นส่วนๆ โดยจัดกลุ่มตามคีย์ที่กำหนด คล้ายกับ groupBy
แต่เคารพลำดับของรายการในคอลเลกชันและนำคีย์ที่มีอยู่กลับมาใช้ใหม่
$ collection = collect ([
[ ' name ' => ' Lesson 1 ' , ' module ' => ' Basics ' ],
[ ' name ' => ' Lesson 2 ' , ' module ' => ' Basics ' ],
[ ' name ' => ' Lesson 3 ' , ' module ' => ' Advanced ' ],
[ ' name ' => ' Lesson 4 ' , ' module ' => ' Advanced ' ],
[ ' name ' => ' Lesson 5 ' , ' module ' => ' Basics ' ],
]);
$ collection -> sectionBy ( ' module ' );
// [
// ['Basics', [
// ['name' => 'Lesson 1', 'module' => 'Basics'],
// ['name' => 'Lesson 2', 'module' => 'Basics'],
// ]],
// ['Advanced', [
// ['name' => 'Lesson 3', 'module' => 'Advanced'],
// ['name' => 'Lesson 4', 'module' => 'Advanced'],
// ]],
// ['Basics', [
// ['name' => 'Lesson 5', 'module' => 'Basics'],
// ]],
// ];
ลายเซ็นแบบเต็ม: sectionBy($callback, $preserveKeys, $sectionKey, $itemsKey)
simplePaginate
สร้างอินสแตนซ์ Paginator
สำหรับรายการในคอลเลกชัน
collect ( $ posts )-> simplePaginate ( 5 );
วิธีนี้จะแบ่งหน้าเนื้อหาของ $posts
ด้วย 5 รายการต่อหน้า simplePaginate
ยอมรับตัวเลือกบางอย่าง ตรงไปที่เอกสาร Laravel เพื่อดูคำแนะนำเชิงลึก
simplePaginate(int $perPage = 15, string $pageName = 'page', ?int $page = null, ?int $total = null, array $options = [])
สำหรับคำแนะนำเชิงลึกเกี่ยวกับการแบ่งหน้า โปรดดูเอกสาร Laravel
sliceBefore
แบ่งค่าออกจากคอลเลกชันก่อนที่การโทรกลับที่กำหนดจะเป็นจริง หากพารามิเตอร์ทางเลือก $preserveKeys
เป็น true
ถูกส่งผ่าน พารามิเตอร์นั้นจะคงคีย์ดั้งเดิมไว้
collect ([ 20 , 51 , 10 , 50 , 66 ])-> sliceBefore ( function ( $ item ) {
return $ item > 50 ;
}); // return collect([[20],[51, 10, 50], [66])
tail
แยกหางออกจากคอลเลกชัน ทุกอย่างยกเว้นองค์ประกอบแรก มันเป็นชวเลขสำหรับ slice(1)->values()
แต่ก็มีประโยชน์มาก หากพารามิเตอร์ทางเลือก $preserveKeys
as true
ถูกส่งผ่าน มันจะรักษาคีย์และทางเลือกกลับไปที่ slice(1)
collect ([ 1 , 2 , 3 ])-> tail (); // return collect([2, 3])
toPairs
แปลงคอลเลกชันให้เป็นอาร์เรย์ด้วยคู่
$ collection = collect ([ ' a ' => ' b ' , ' c ' => ' d ' , ' e ' => ' f ' ])-> toPairs ();
$ collection -> toArray (); // returns ['a', 'b'], ['c', 'd'], ['e', 'f']
transpose
เป้าหมายของการย้ายคือการหมุนอาร์เรย์หลายมิติ โดยเปลี่ยนแถวเป็นคอลัมน์ และเปลี่ยนคอลัมน์ให้เป็นแถว
collect ([
[ ' Jane ' , ' Bob ' , ' Mary ' ],
[ ' [email protected] ' , ' [email protected] ' , ' [email protected] ' ],
[ ' Doctor ' , ' Plumber ' , ' Dentist ' ],
])-> transpose ()-> toArray ();
// [
// ['Jane', '[email protected]', 'Doctor'],
// ['Bob', '[email protected]', 'Plumber'],
// ['Mary', '[email protected]', 'Dentist'],
// ]
try
หากมีวิธีการใด ๆ ระหว่าง try
และ catch
Throw ข้อยกเว้น ข้อยกเว้นก็สามารถจัดการได้ใน catch
collect ([ ' a ' , ' b ' , ' c ' , 1 , 2 , 3 ])
-> try ()
-> map ( fn ( $ letter ) => strtoupper ( $ letter ))
-> each ( function () {
throw new Exception ( ' Explosions in the sky ' );
})
-> catch ( function ( Exception $ exception ) {
// handle exception here
})
-> map ( function () {
// further operations can be done, if the exception wasn't rethrow in the `catch`
});
แม้ว่าวิธีการต่างๆ จะตั้งชื่อว่า try
/ catch
เพื่อความคุ้นเคยกับ PHP แต่คอลเลกชันเองก็มีพฤติกรรมเหมือนกับธุรกรรมฐานข้อมูลมากกว่า ดังนั้นเมื่อมีการโยนข้อยกเว้น คอลเลกชันดั้งเดิม (ก่อนการลอง) จะถูกส่งคืน
คุณสามารถเข้าถึงคอลเลกชันภายใน catch ได้โดยการเพิ่มพารามิเตอร์ตัวที่สองให้กับตัวจัดการของคุณ คุณยังสามารถจัดการคอลเลกชันภายใน catch โดยการส่งคืนค่า
$ collection = collect ([ ' a ' , ' b ' , ' c ' , 1 , 2 , 3 ])
-> try ()
-> map ( function ( $ item ) {
throw new Exception ();
})
-> catch ( function ( Exception $ exception , $ collection ) {
return collect ([ ' d ' , ' e ' , ' f ' ]);
})
-> map ( function ( $ item ) {
return strtoupper ( $ item );
});
// ['D', 'E', 'F']
validate
คืนค่า true
หาก $callback
ที่ให้คืนค่าจริงสำหรับทุกรายการ หาก $callback
เป็นสตริงหรืออาร์เรย์ ให้ถือว่าเป็นกฎการตรวจสอบ
collect ([ ' foo ' , ' foo ' ])-> validate ( function ( $ item ) {
return $ item === ' foo ' ;
}); // returns true
collect ([ ' [email protected] ' , ' bla ' ])-> validate ( ' email ' ); // returns false
collect ([ ' [email protected] ' , ' [email protected] ' ])-> validate ( ' email ' ); // returns true
weightedRandom
ส่งคืนไอเท็มแบบสุ่มตามน้ำหนัก ในตัวอย่างนี้ สินค้าที่มี a
มีโอกาสถูกเลือกมากที่สุด และสินค้าที่มี c
น้อยที่สุด
// pass the field name that should be used as a weight
$ randomItem = collect ([
[ ' value ' => ' a ' , ' weight ' => 30 ],
[ ' value ' => ' b ' , ' weight ' => 20 ],
[ ' value ' => ' c ' , ' weight ' => 10 ],
])-> weightedRandom ( ' weight ' );
หรือคุณสามารถส่งผ่านสายเพื่อรับน้ำหนักได้
$ randomItem = collect ([
[ ' value ' => ' a ' , ' weight ' => 30 ],
[ ' value ' => ' b ' , ' weight ' => 20 ],
[ ' value ' => ' c ' , ' weight ' => 10 ],
])-> weightedRandom ( function ( array $ item ) {
return $ item [ ' weight ' ];
});
withSize
สร้างคอลเลกชันใหม่ด้วยจำนวนรายการที่ระบุ
Collection:: withSize ( 1 )-> toArray (); // return [1];
Collection:: withSize ( 5 )-> toArray (); // return [1,2,3,4,5];
โปรดดู CHANGELOG สำหรับข้อมูลเพิ่มเติมว่ามีอะไรเปลี่ยนแปลงเมื่อเร็วๆ นี้
$ composer test
โปรดดูการมีส่วนร่วมเพื่อดูรายละเอียด
หากคุณพบข้อบกพร่องเกี่ยวกับการรักษาความปลอดภัย โปรดส่งอีเมลมาที่ [email protected] แทนการใช้ตัวติดตามปัญหา
Spatie เป็นเอเจนซี่ออกแบบเว็บไซต์ที่ตั้งอยู่ในเมืองแอนต์เวิร์ป ประเทศเบลเยียม คุณจะพบภาพรวมของโครงการโอเพ่นซอร์สทั้งหมดของเราบนเว็บไซต์ของเรา
ใบอนุญาตเอ็มไอที (MIT) โปรดดูไฟล์ใบอนุญาตสำหรับข้อมูลเพิ่มเติม