PHPackages                             ibrostudio/laravel-data-repository - 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. ibrostudio/laravel-data-repository

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

ibrostudio/laravel-data-repository
==================================

Laravel data manager

1.24.0(1y ago)11491MITPHPPHP ^8.4

Since Jun 25Pushed 1y ago1 watchersCompare

[ Source](https://github.com/iBroStudio/laravel-data-repository)[ Packagist](https://packagist.org/packages/ibrostudio/laravel-data-repository)[ Docs](https://github.com/ibrostudio/laravel-data-repository)[ RSS](/packages/ibrostudio-laravel-data-repository/feed)WikiDiscussions main Synced yesterday

READMEChangelog (10)Dependencies (21)Versions (35)Used By (1)

Laravel Data Objects Repository
===============================

[](#laravel-data-objects-repository)

Save Data Transfer Objects (from Spatie's [Laravel Data](https://github.com/spatie/laravel-data)) and Value Objects (from Michael Rubel's [Laravel Value Objects](https://github.com/michael-rubel/laravel-value-objects)) in database and attach to eloquent models.

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

[](#installation)

Install the package via composer:

```
composer require ibrostudio/laravel-data-objects-repository
```

Then run the installer:

```
php artisan data-repository:install
```

Usage
-----

[](#usage)

Add the trait `IBroStudio\DataRepository\Concerns\HasDataRepository` to your Eloquent models.

The trait implements a `MorphManyDataObjects` relationship that extends `MorphMany`.

```
namespace App\Models;

use IBroStudio\DataRepository\Concerns\HasDataRepository;
use Illuminate\Database\Eloquent\Model;

class YourEloquentModel extends Model
{
    use HasDataRepository;
}
```

### Working with DTO

[](#working-with-dto)

Create your DTO following Spatie's Laravel Data [documentation](https://spatie.be/docs/laravel-data/v4/as-a-data-transfer-object/creating-a-data-object).

```
namespace App\DataObjects;

use Spatie\LaravelData\Data;

class SongData extends Data
{
    public function __construct(
        public string $title,
        public string $artist,
    ) {
    }
}
```

```
$data = new SongData(
    title: 'Walk',
    artist: 'Pantera'
);
```

### Working with Value Objects

[](#working-with-value-objects)

Use built-in Value Objects or create one following Michael Rubel's Value Objects [documentation](https://github.com/michael-rubel/laravel-value-objects).

```
$data = new \MichaelRubel\ValueObjects\Collection\Complex\Name('Pantera');
```

Save the Value Object attached to the model in database:

```
$model->data_repository()->add($data);
```

For more complex usage, you can use Value Objects in DTO:

```
namespace App\DataObjects;

use Spatie\LaravelData\Data;
use MichaelRubel\ValueObjects\Collection\Complex\Name;

class SongData extends Data
{
    public function __construct(
        public Name $title,
        public Name $artist,
    ) {
    }
}
```

#### Creating or updating objects

[](#creating-or-updating-objects)

The following method save the object in database and attach it to the model:

```
$model->data_repository()->add($data);
```

If an object with the sans data class is already attached to the model, its values will be replaced.

#### Unique data class

[](#unique-data-class)

You can attach many objects to a model but only one for each data class by default. Considering the previous examples, the model can have only one SongData object (and other DTO or Value Objects).

#### Multiple data class

[](#multiple-data-class)

If you need more than one object from the same data class, use the `valuesAttributes` parameter when you create or update an object:

```
$song1 = new SongData(
    title: 'Walk',
    artist: 'Pantera'
);

$model->data_repository()->add($data);

$song2 = new SongData(
    title: 'Cowboys From Hell',
    artist: 'Pantera'
);

$model->data_repository()->add(
    data: $song2,
    valuesAttributes: [
        'values->title' => $song2->title,
    ]
);

$song3 = new SongData(
    title: 'Davidian',
    artist: 'Machine Head'
);

$model->data_repository()->add(
    data: $song3,
    valuesAttributes: [
        'values->artist' => $song3->artist,
    ]
);
```

### Retrieving objects

[](#retrieving-objects)

You access to all `MorphManyDataObjects` relations for a model with:

```
$model->data_repository();
```

You can limit the instance to a specific data class to access a single object:

```
$model->data_repository(dataClass: SongData::class);
```

The object is then retrieved by the `values()` method:

```
$song = $model->data_repository(dataClass: SongData::class)->values();
```

If necessary, you can also constrain the retrieval of the object to certain values of this object with the `valuesQuery` parameter:

```
$model->data_repository(
    dataClass: SongData::class,
    valuesQuery: ['title' => 'Walk']
);
```

Eloquent attribute casting
--------------------------

[](#eloquent-attribute-casting)

You can get direct access to an object value like `$model->song` instead of `$model->data_repository(dataClass: SongData::class)`.

Add a `unsignedBigInteger` column to your model:

```
$table->unsignedBigInteger('song')->nullable();
```

And then add the cast to the model class:

```
use IBroStudio\DataRepository\EloquentCasts\DataObjectCast;

class YourEloquentModel extends Model
{
    protected function casts(): array
    {
        return [
            'song' => DataObjectCast::class,
        ];
    }
}
```

Usage:

```
use IBroStudio\DataRepository\EloquentCasts\DataObjectCast;

class YourEloquentModel extends Model
{
    protected function casts(): array
    {
        return [
            'song' => DataObjectCast::class,
        ];
    }
}

$model = YourEloquentModel::create([
    'song' => new SongData(
        title: 'Walk',
        artist: 'Pantera'
    ),
]);
// or
$model->song = new SongData(
    title: 'Walk',
    artist: 'Pantera'
);

$model->save();
```

You can constrain the property to a defined data class by adding it to the cast:

```
use IBroStudio\DataRepository\EloquentCasts\DataObjectCast;

class YourEloquentModel extends Model
{
    protected function casts(): array
    {
        return [
            'song' => DataObjectCast::class.':'.SongData::class,
        ];
    }
}
```

This adds data validation and automatic instantiation of the data class, allowing you to simply pass an array to the property setter:

```
$model = YourEloquentModel::create([
    'song' => [
        'title' => 'Walk',
        'artist' => 'Pantera'
    ]
]);
```

Built-in Objects Values
-----------------------

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

- ByteUnit
- EncryptableText
- GitSshUrl
- HashedPassword
- IpAddress
- SemanticVersion
- Timecode
- VersionedComposerJson
- Name, FirstName, LastName, Fullname, CompanyName
- Email
- Phone
- TaxNumber

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

License
-------

[](#license)

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

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance44

Moderate activity, may be stable

Popularity12

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 95.8% 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 ~9 days

Recently: every ~1 days

Total

34

Last Release

442d ago

PHP version history (2 changes)v1.0.0PHP ^8.2

1.24.0PHP ^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/3a456dae716df4be38cb7d5f02ca522a02ec37eda586ab0cae0f504516b7c1ef?d=identicon)[iBroStudio](/maintainers/iBroStudio)

---

Top Contributors

[![Yann-iBroStudio](https://avatars.githubusercontent.com/u/2676572?v=4)](https://github.com/Yann-iBroStudio "Yann-iBroStudio (91 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (2 commits)")

---

Tags

laraveldata-transfer-objectdtoiBroStudioobject value

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/ibrostudio-laravel-data-repository/health.svg)

```
[![Health](https://phpackages.com/badges/ibrostudio-laravel-data-repository/health.svg)](https://phpackages.com/packages/ibrostudio-laravel-data-repository)
```

###  Alternatives

[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.8M47](/packages/spatie-laravel-pdf)[codewithdennis/filament-select-tree

The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.

329530.5k29](/packages/codewithdennis-filament-select-tree)[tarfin-labs/event-machine

Event-driven state machines for Laravel with event sourcing, type-safe context, and full audit trail.

199.4k](/packages/tarfin-labs-event-machine)[relaticle/custom-fields

User Defined Custom Fields for Laravel Filament

16354.2k](/packages/relaticle-custom-fields)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3914.6k](/packages/rawilk-profile-filament-plugin)[danestves/laravel-polar

A package to easily integrate your Laravel application with Polar.sh

8120.4k](/packages/danestves-laravel-polar)

PHPackages © 2026

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