44
55use ArgumentCountError ;
66use ArrayAccess ;
7+ use Closure ;
78use Illuminate \Contracts \Support \Arrayable ;
89use Illuminate \Contracts \Support \Jsonable ;
910use Illuminate \Support \Traits \Macroable ;
@@ -686,8 +687,8 @@ public static function select($array, $keys)
686687 * Pluck an array of values from an array.
687688 *
688689 * @param iterable $array
689- * @param string|array|int|null $value
690- * @param string|array|null $key
690+ * @param string|array|int|Closure| null $value
691+ * @param string|array|Closure| null $key
691692 * @return array
692693 */
693694 public static function pluck ($ array , $ value , $ key = null )
@@ -697,15 +698,19 @@ public static function pluck($array, $value, $key = null)
697698 [$ value , $ key ] = static ::explodePluckParameters ($ value , $ key );
698699
699700 foreach ($ array as $ item ) {
700- $ itemValue = data_get ($ item , $ value );
701+ $ itemValue = $ value instanceof Closure
702+ ? $ value ($ item )
703+ : data_get ($ item , $ value );
701704
702705 // If the key is "null", we will just append the value to the array and keep
703706 // looping. Otherwise we will key the array using the value of the key we
704707 // received from the developer. Then we'll return the final array form.
705708 if (is_null ($ key )) {
706709 $ results [] = $ itemValue ;
707710 } else {
708- $ itemKey = data_get ($ item , $ key );
711+ $ itemKey = $ key instanceof Closure
712+ ? $ key ($ item )
713+ : data_get ($ item , $ key );
709714
710715 if (is_object ($ itemKey ) && method_exists ($ itemKey , '__toString ' )) {
711716 $ itemKey = (string ) $ itemKey ;
@@ -721,15 +726,15 @@ public static function pluck($array, $value, $key = null)
721726 /**
722727 * Explode the "value" and "key" arguments passed to "pluck".
723728 *
724- * @param string|array $value
725- * @param string|array|null $key
729+ * @param string|array|Closure $value
730+ * @param string|array|Closure| null $key
726731 * @return array
727732 */
728733 protected static function explodePluckParameters ($ value , $ key )
729734 {
730735 $ value = is_string ($ value ) ? explode ('. ' , $ value ) : $ value ;
731736
732- $ key = is_null ($ key ) || is_array ($ key ) ? $ key : explode ('. ' , $ key );
737+ $ key = is_null ($ key ) || is_array ($ key ) || $ key instanceof Closure ? $ key : explode ('. ' , $ key );
733738
734739 return [$ value , $ key ];
735740 }
0 commit comments