PHPackages                             michaelravedoni/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. [Database &amp; ORM](/categories/database)
4. /
5. michaelravedoni/laravel-value-objects

ActiveLibrary[Database &amp; ORM](/categories/database)

michaelravedoni/laravel-value-objects
=====================================

A base package for creating value objects in Laravel, with Artisan command and generic Eloquent cast.

1.0.0(10mo ago)03MITPHPPHP ^8.1CI passing

Since Jul 9Pushed 10mo agoCompare

[ Source](https://github.com/michaelravedoni/laravel-value-objects)[ Packagist](https://packagist.org/packages/michaelravedoni/laravel-value-objects)[ Docs](https://github.com/michaelravedoni/laravel-value-objects)[ RSS](/packages/michaelravedoni-laravel-value-objects/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (6)Versions (2)Used By (0)

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

[](#laravel-value-objects)

 **Simply create value objects in Laravel.**

###  [Documentation](https://github.com/michaelravedoni/laravel-value-objects#documentation)  |  [ Contributing ](#contributing)

[](#----documentation----------------contributing------)

 Built with ❤︎ by [Michael Ravedoni](https://michael.ravedoni.com/en) and [ contributors ](https://github.com/michaelravedoni/prathletics/contributors)

Introduction
------------

[](#introduction)

[![Latest Version](https://camo.githubusercontent.com/7a07f7151231535618c7844d6c934d0b927b7050d4ec5ce476437c46e42e1d5a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6d69636861656c72617665646f6e692f6c61726176656c2d76616c75652d6f626a656374732e7376673f7374796c653d666c61742d737175617265)](https://github.com/michaelravedoni/laravel-value-objects/releases)[![GitHub Workflow Status](https://camo.githubusercontent.com/5595e7c038a8b7ee9c4663e1c3346f770d54b9ef54a3c2bf86e6e490b268ec74/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d69636861656c72617665646f6e692f6c61726176656c2d76616c75652d6f626a656374732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473)](https://camo.githubusercontent.com/5595e7c038a8b7ee9c4663e1c3346f770d54b9ef54a3c2bf86e6e490b268ec74/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d69636861656c72617665646f6e692f6c61726176656c2d76616c75652d6f626a656374732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/ffe14a2069ae1b4155e47a38716515c323f41cc65a2b1f1b3c7567d1b8852814/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d69636861656c72617665646f6e692f6c61726176656c2d76616c75652d6f626a656374732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/michaelravedoni/laravel-value-objects)

A simple, lightweight Laravel package for creating value objects, including an Artisan command for generating them and a generic Eloquent *casting* system.

✨ Features
----------

[](#-features)

- **Basic Value Object**: An abstract class for structuring your value objects.
- **Contractual interface**: For strong typing and consistent implementation.
- **Generic Eloquent Casting**: Easily converts model attributes into instances of your custom value objects and vice versa.
- **Artisan command**: Quickly generates value object skeletons.
- **Built-in validation**: Add your validation logic directly into the constructor of your value objects.
- **Extensive compatibility**: Supports Laravel 10, 11, 12 and PHP 8.2, 8.3, 8.4.

🚀 Installation
--------------

[](#-installation)

This package can be installed through Composer :

```
composer require michaelravedoni/laravel-value-objects
```

The package will be automatically discovered by Laravel.

💡 Documentation
---------------

[](#-documentation)

### Creating a Value Object

[](#creating-a-value-object)

The easiest way to create a new value object is to use the Artisan command provided by the :

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

This will create an `app/ValueObjects/MyCustomValue.php` file with a code skeleton.

You then need to implement the constructor to define the value and add your validation logic, as well as the `value()` method to return the raw value.

**Example: An `Email` value object**.

```
// app/ValueObjects/Email.php
