mapWithKeys() public method

The callback should return an associative array with a single key/value pair.
public mapWithKeys ( callable $callback ) : static
$callback callable
return static
Example #1
0
 public function toArray()
 {
     return $this->attributes->mapWithKeys(function ($item, $key) {
         if (is_subclass_of($item, BaseType::class)) {
             /** @var BaseType $item */
             return [$key => $item->toArray()];
         }
         if (is_object($item) && get_class($item) === Carbon::class) {
             /** @var $item Carbon */
             return [$key => $item->timestamp];
         }
         return [$key => $item];
     })->toArray();
 }