Download the PHP package derheyne/laravel-collection-mapwithcast without Composer
On this page you can find all versions of the php package derheyne/laravel-collection-mapwithcast. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download derheyne/laravel-collection-mapwithcast
More information about derheyne/laravel-collection-mapwithcast
Files in derheyne/laravel-collection-mapwithcast
Package laravel-collection-mapwithcast
Short Description This is my package laravel-collection-mapwithcast
License MIT
Homepage https://github.com/derheyne/laravel-collection-mapwithcast
Informations about the package laravel-collection-mapwithcast
Laravel Collection Macro: mapWithCast
Automatically cast values in Laravel collections when using mapWithCast
with typed closures.
๐ฆ About
mapWithCast
is a Laravel collection macro that enhances the map method by automatically casting each item in the
collection to the type hinted in your closure. It saves you from manual casting and enforces better type safety, making
your code cleaner and more expressive.
It supports both scalar types like int
and string
and complex Laravel-specific types like Collection
, Fluent
, and
Stringable
.
๐ Installation
Install the package via Composer:
The macro will be automatically registered thanks to Laravel's package discovery.
๐ง Type Support
โ Supported Types (Out of the Box)
int
float
bool
string
array
object
Illuminate\Support\Carbon
andCarbon\Carbon
Illuminate\Support\Collection
Illuminate\Support\Fluent
Illuminate\Support\Optional
Illuminate\Support\Stringable
Illuminate\Support\Uri
โ๏ธ Extending with Custom Casters
Need to handle your own types or custom logic? You can register additional casters by publishing the config file:
This will create a config file where you can specify your own custom casters:
Now you can automatically cast the value into the specified laravel-data DTO:
๐ Examples
๐งฎ Convert and Process Numbers
๐ง Cast to Laravel Collection
๐ Cast to Stringable
๐ชก Cast by specifying a custom caster
โ Testing
๐งช Compatibility
- Laravel 11.x, 12.x
- PHP 8.3+
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-collection-mapwithcast with dependencies
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^11.35||^12.0