PHPackages                             michael-rubel/laravel-value-objects - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. michael-rubel/laravel-value-objects

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

michael-rubel/laravel-value-objects
===================================

It is an example template for Laravel packages. Fill or change it the way you like.

7.0.2(1y ago)20136.8k↓20.8%17[2 PRs](https://github.com/michael-rubel/laravel-value-objects/pulls)2MITPHPPHP ^8.1CI passing

Since Sep 23Pushed 1y ago3 watchersCompare

[ Source](https://github.com/michael-rubel/laravel-value-objects)[ Packagist](https://packagist.org/packages/michael-rubel/laravel-value-objects)[ Docs](https://github.com/michael-rubel/laravel-value-objects)[ Fund](https://paypal.com/donate/?hosted_button_id=KHLEL8PFS4AXJ)[ RSS](/packages/michael-rubel-laravel-value-objects/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (12)Versions (33)Used By (2)

[![Value Objects for Laravel](https://user-images.githubusercontent.com/37669560/200172635-6b2ca8d8-fb2b-4037-a697-b8f6e4c8c615.png)](https://user-images.githubusercontent.com/37669560/200172635-6b2ca8d8-fb2b-4037-a697-b8f6e4c8c615.png)

Laravel Value Objects
=====================

[](#laravel-value-objects)

[![Latest Version on Packagist](https://camo.githubusercontent.com/13e0eff286343c3a088f0074e8b664f3d63e465a5e3e4b5455f3681aaebc7d6f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d69636861656c2d727562656c2f6c61726176656c2d76616c75652d6f626a656374732e7376673f7374796c653d666c61742d737175617265266c6f676f3d7061636b6167697374)](https://packagist.org/packages/michael-rubel/laravel-value-objects)[![Tests](https://camo.githubusercontent.com/eefa2ffc85d7ea0f9446de37c81cc3e457f86467dfdff609398f0ca07a845286/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d69636861656c2d727562656c2f6c61726176656c2d76616c75652d6f626a656374732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265266c6162656c3d7465737473266c6f676f3d676974687562)](https://github.com/michael-rubel/laravel-value-objects/actions)[![Code Quality](https://camo.githubusercontent.com/2595137c86081f52e5869243a9cc3d9f677ed461cd1e086ca753abef5b07b40c/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f7175616c6974792f672f6d69636861656c2d727562656c2f6c61726176656c2d76616c75652d6f626a656374732e7376673f7374796c653d666c61742d737175617265266c6f676f3d7363727574696e697a6572)](https://scrutinizer-ci.com/g/michael-rubel/laravel-value-objects/?branch=main)[![Code Coverage](https://camo.githubusercontent.com/d0e311c853c91472aee9fb2a5ff38f7fe2aef037c6458063b4a981d11dcb5cde/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6d69636861656c2d727562656c2f6c61726176656c2d76616c75652d6f626a656374732e7376673f7374796c653d666c61742d737175617265266c6f676f3d7363727574696e697a6572)](https://scrutinizer-ci.com/g/michael-rubel/laravel-value-objects/?branch=main)[![Infection](https://camo.githubusercontent.com/4324d661d69af22d495b4df936b6770d20007b85bf09d1a7ef18c4811d9c8991/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d69636861656c2d727562656c2f6c61726176656c2d76616c75652d6f626a656374732f696e66656374696f6e2e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265266c6162656c3d696e66656374696f6e266c6f676f3d706870)](https://github.com/michael-rubel/laravel-value-objects/actions)[![Larastan](https://camo.githubusercontent.com/a54736facfcea789252a917eb9403359d2730e7a16ada2f68360a1fd3ad77264/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d69636861656c2d727562656c2f6c61726176656c2d76616c75652d6f626a656374732f7068707374616e2e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265266c6162656c3d6c6172617374616e266c6f676f3d6c61726176656c)](https://github.com/michael-rubel/laravel-value-objects/actions)

A bunch of general-purpose value objects you can use in your Laravel application.

---

The package requires `PHP 8.1` or higher and `Laravel 10` or higher.

\#StandWithUkraine
------------------

[](#standwithukraine)

[![SWUbanner](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md)

Installation
------------

[](#installation)

Install the package using composer:

```
composer require michael-rubel/laravel-value-objects
```

Built-in value objects
----------------------

[](#built-in-value-objects)

- [`Boolean`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Primitive/Boolean.php)
- [`ClassString`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/ClassString.php)
- [`Email`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/Email.php)
- [`FullName`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/FullName.php)
- [`Name`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/Name.php)
- [`Number`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Primitive/Number.php)
- [`Phone`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/Phone.php)
- [`TaxNumber`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/TaxNumber.php)
- [`Text`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Primitive/Text.php)
- [`Url`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/Url.php)
- [`Uuid`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/Uuid.php)

### Artisan command

[](#artisan-command)

You can generate custom value objects with Artisan command:

```
php artisan make:value-object YourNameValueObject
```

Usage
-----

[](#usage)

### Boolean

[](#boolean)

```
$bool = new Boolean('1');
$bool = Boolean::make('1');
$bool = Boolean::from('1');

$bool->value();   // true
(string) $bool;   // 'true'
$bool->toArray(); // ['true']
```

---

### Number

[](#number)

```
$number = new Number('10.20999', scale: 2);
$number = Number::make('10.20999', scale: 2);
$number = Number::from('10.20999', scale: 2);

$number->value();   // '10.20'
(string) $number;   // '10.20'
$number->toArray(); // ['10.20']

// Starting from version `3.5.0` also
// accepts locale-formatted numbers:
$number = new Number('1.230,00');
$number->value(); // '1230.00'
$number = new Number('1,230.00');
$number->value(); // '1230.00'
$number = new Number('1 230,00');
$number->value(); // '1230.00'
$number = new Number('1 230.00');
$number->value(); // '1230.00'
```

---

### Text

[](#text)

```
$text = new Text('Lorem Ipsum is simply dummy text.');
$text = Text::make('Lorem Ipsum is simply dummy text.');
$text = Text::from('Lorem Ipsum is simply dummy text.');

$text->value();   // 'Lorem Ipsum is simply dummy text.'
(string) $text;   // 'Lorem Ipsum is simply dummy text.'
$text->toArray(); // ['Lorem Ipsum is simply dummy text.']
```

---

### ClassString

[](#classstring)

```
$classString = new ClassString('\Exception');
$classString = ClassString::make('\Exception');
$classString = ClassString::from('\Exception');

$classString->value();   // '\Exception'
(string) $classString;   // '\Exception'
$classString->toArray(); // ['\Exception']

$classString->classExists();     // true
$classString->interfaceExists(); // false
$classString->instantiate();     // Exception { ... }
$classString->instantiateWith(['message' => 'My message.']); // Exception { #message: "test" ... }
```

---

### Email

[](#email)

```
$email = new Email('michael@laravel.software');
$email = Email::make('michael@laravel.software');
$email = Email::from('michael@laravel.software');

$email->value();   // 'michael@laravel.software'
(string) $email;   // 'michael@laravel.software'
$email->toArray(); // ['email' => 'michael@laravel.software', 'username' => 'michael', 'domain' => 'laravel.software']
```

---

### FullName

[](#fullname)

```
$name = new FullName(' Taylor   Otwell ');
$name = FullName::make(' Taylor   Otwell ');
$name = FullName::from(' Taylor   Otwell ');

$name->value();   // 'Taylor Otwell'
(string) $name;   // 'Taylor Otwell'

$name->fullName();  // 'Taylor Otwell'
$name->firstName(); // 'Taylor'
$name->lastName();  // 'Otwell'

$name = 'Richard Le Poidevin';

$fullName = new FullName($name, limit: 2);

$fullName->toArray();

// array:3 [
//  "fullName" => "Richard Le Poidevin"
//  "firstName" => "Richard"
//  "lastName" => "Le Poidevin"
// ]
```

---

### Name

[](#name)

```
$name = new Name(' Company name! ');
$name = Name::make(' Company name! ');
$name = Name::from(' Company name! ');

$name->value();   // 'Company name!'
(string) $name;   // 'Company name!'
$name->toArray(); // ['Company name!']
```

---

### Phone

[](#phone)

```
$phone = new Phone(' +38 000 000 00 00 ');
$phone = Phone::make(' +38 000 000 00 00 ');
$phone = Phone::from(' +38 000 000 00 00 ');

$phone->value();   // '+38 000 000 00 00'
(string) $phone;   // '+38 000 000 00 00'
$phone->toArray(); // ['+38 000 000 00 00']

$phone->sanitized(); // '+380000000000'
```

---

### TaxNumber

[](#taxnumber)

```
$taxNumber = new TaxNumber('0123456789', 'PL');
$taxNumber = TaxNumber::make('0123456789', 'PL');
$taxNumber = TaxNumber::from('0123456789', 'PL');

$taxNumber->value();   // 'PL0123456789'
(string) $taxNumber;   // 'PL0123456789'
$taxNumber->toArray(); // ['fullTaxNumber' => 'PL0123456789', 'taxNumber' => '0123456789', 'prefix' => 'PL']

$taxNumber->fullTaxNumber(); // 'PL0123456789'
$taxNumber->taxNumber();     // '0123456789'
$taxNumber->prefix();        // 'PL'
```

---

### Url

[](#url)

```
$uuid = new Url('my-blog-page');
$uuid = Url::make('my-blog-page');
$uuid = Url::from('my-blog-page');

$uuid->value();   // 'https://example.com/my-blog-page'
(string) $uuid;   // 'https://example.com/my-blog-page'
$uuid->toArray(); // ['https://example.com/my-blog-page']
```

---

### Uuid

[](#uuid)

```
$uuid = new Uuid('8547d10c-7a37-492a-8d33-be0e5ae6119b', 'Optional name');
$uuid = Uuid::make('8547d10c-7a37-492a-8d33-be0e5ae6119b', 'Optional name');
$uuid = Uuid::from('8547d10c-7a37-492a-8d33-be0e5ae6119b', 'Optional name');

$uuid->value();   // '8547d10c-7a37-492a-8d33-be0e5ae6119b'
(string) $uuid;   // '8547d10c-7a37-492a-8d33-be0e5ae6119b'
$uuid->toArray(); // ['name' => 'Optional name', 'value' => '8547d10c-7a37-492a-8d33-be0e5ae6119b']

$uuid->uuid(); // '8547d10c-7a37-492a-8d33-be0e5ae6119b'
$uuid->name(); // 'Optional name'
```

Handle failed validation
------------------------

[](#handle-failed-validation)

If you want to avoid try/catching your value object when the validation fails, you can use `makeOrNull` method:

```
$bool = Boolean::makeOrNull('bad input'); // null

$bool?->value(); // null
```

Extending functionality
-----------------------

[](#extending-functionality)

All value objects are [Macroable](https://laravel.com/api/9.x/Illuminate/Support/Traits/Macroable.html). This way you can add new methods dynamically. If you need to extend existing methods, you can create a value object locally with `make:value-object` command and use inheritance.

For example:

```
ValueObject::macro('str', function () {
    return str($this->value());
});

$name = new Text('Lorem ipsum');

$name->str()->is('Lorem ipsum'); // true
```

Conditionable
-------------

[](#conditionable)

Value objects utilize a [Conditionable](https://laravel.com/api/9.x/Illuminate/Support/Traits/Conditionable.html) trait. You can use `when` and `unless` methods.

```
TaxNumber::from('PL0123456789')->when(function ($number) {
    return $number->prefix() !== null;
})->prefix();
```

Contributing
------------

[](#contributing)

If you see any way we can improve the package, or maybe you want to make your own custom value object as built-in, PRs are welcome.

Testing
-------

[](#testing)

```
composer test
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance42

Moderate activity, may be stable

Popularity45

Moderate usage in the ecosystem

Community23

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 94.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~27 days

Recently: every ~105 days

Total

30

Last Release

537d ago

Major Versions

2.1.0 → 3.0.02022-10-31

3.5.0 → 4.0.02022-11-12

4.2.1 → 5.0.02022-12-16

5.1.1 → 6.0.02023-11-26

6.0.0 → 7.0.02024-03-14

PHP version history (2 changes)0.0.1PHP ^8.0

7.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/9c060c69332d50a7cb290529ad84a7f216dec94d59950db7446dc93208787083?d=identicon)[michael-rubel](/maintainers/michael-rubel)

---

Top Contributors

[![michael-rubel](https://avatars.githubusercontent.com/u/37669560?v=4)](https://github.com/michael-rubel "michael-rubel (393 commits)")[![olsza](https://avatars.githubusercontent.com/u/12556170?v=4)](https://github.com/olsza "olsza (11 commits)")[![crynobone](https://avatars.githubusercontent.com/u/172966?v=4)](https://github.com/crynobone "crynobone (5 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")[![eaborodkin](https://avatars.githubusercontent.com/u/14907535?v=4)](https://github.com/eaborodkin "eaborodkin (1 commits)")[![bryanlopezinc](https://avatars.githubusercontent.com/u/23613427?v=4)](https://github.com/bryanlopezinc "bryanlopezinc (1 commits)")[![ramnzys](https://avatars.githubusercontent.com/u/19354043?v=4)](https://github.com/ramnzys "ramnzys (1 commits)")

---

Tags

laravellooking-for-contributorsphpvalue-objectvalue-objectslaravelmichael-rubellaravel-value-objects

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/michael-rubel-laravel-value-objects/health.svg)

```
[![Health](https://phpackages.com/badges/michael-rubel-laravel-value-objects/health.svg)](https://phpackages.com/packages/michael-rubel-laravel-value-objects)
```

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.8k28.9M627](/packages/spatie-laravel-data)[hirethunk/verbs

An event sourcing package that feels nice.

513162.9k6](/packages/hirethunk-verbs)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
