PHPackages                             dan-har/presentit-laravel - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. dan-har/presentit-laravel

ActiveLibrary[HTTP &amp; Networking](/categories/http)

dan-har/presentit-laravel
=========================

Transformers for nested Eloquent models and collection for building API with Laravel framework

1.0.1(8y ago)029MITPHPPHP &gt;=5.5.9

Since Mar 18Pushed 8y ago1 watchersCompare

[ Source](https://github.com/dan-har/presentit-laravel)[ Packagist](https://packagist.org/packages/dan-har/presentit-laravel)[ RSS](/packages/dan-har-presentit-laravel/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (4)Versions (3)Used By (0)

presentit-laravel
=================

[](#presentit-laravel)

Presentit adapter for laravel framework.

Custom presentations and transformation of nested Eloquent models, models relations and collections.

See full presentit docs [here](https://github.com/dan-har/presentit)

Docs
====

[](#docs)

- [Installation](#installation)
- [Transform Eloquent models](#transform-eloquent-models)
- [Transform collections](#transform-collections)
- [Transform nested models and relations](#transform-nested-models-and-relations)

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

[](#installation)

Install using composer

```
composer require dan-har/presentit-laravel

```

Add the presentit service provider to the app config file

```
'providers' => [
    // ...
    Presentit\Laravel\PresentitServiceProvider::class,
]
```

Transform Eloquent models
-------------------------

[](#transform-eloquent-models)

Use presentit transformation functionality with any Eloquent model by implementing the `Presentable` contract and using the `PresentsItem` trait.

For example, the User model class with the PresentsItem trait

```
class User extends Authenticatable implements Presentable
{
    use PresentsItem;

    //...
}
```

To transform the user model use the `present` method to get a `Present` instance or use the `transfrom` method to use a transformer.

```
$user = User::find(1);

$user->present()->with(function(User $user){
    return [
        //...
    ];
});

$user->transform(function(User $user){
    return [
        //...
    ];
});
```

Instead of closure transformer you can pass a transformer class, see presentit docs or example below.

Transform collections
---------------------

[](#transform-collections)

To transform collections the `present` and `transformWith` macros were added to the base collection.

```
$posts = Collection::make();

$posts->present()->each(function (Post $post) {
    return [
        //...
    ];
});

$posts->transformWith(function (Post $post) {
    return [
        //...
    ];
});
```

Model relations that returns collections such as HasMany will also have the presentit transformation functionality

> The collection presentit api uses the `transformWith` method because the `transform` method exists in the base laravel collection.

```
class Post implements Presentable
{
    use PresentsItem;

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

$posts = Posts::find(1);

$posts->comments->transformWith(function (Comment $comment) {
    return [
        //...
    ];
});
```

Transform nested models and relations
-------------------------------------

[](#transform-nested-models-and-relations)

To demonstrate the nested model transformation we will use an example of a Post with comments and on each comment users can write comments. So first we use a transformer class for the Post, Comment and User model

```
class UserTransformer
{
    public function transform(User $user) {
        return [
            'name' => ucfirst($user->name),
            'profile_image' => $user->profile_image ?: Hidden::key(),
        ];
    }
}

class CommentTransformer
{
    public function transform(Comment $comment)
    {
        return [
            'text' => $comment->text,
            'datetime' => $comment->created_at->toW3cString(),
            'edited_datetime' => $comment->edited_at ? $comment->edited_at : Hidden::key(),
            'user' => $comment->user->transform(UserTransformer::class),
            'comments' => $comment->comments->transformWith(CommentTransformer::class),
        ];
    }
}

class PostTransformer
{
    public function tranfrom(Post $post)
    {
        return [
            'title' => $post->title,
            'text' => $post->text,
            'user' => $post->user->transform(UserTransformer::class),
            'datetime' => $post->created_at->toW3cString(),
            'comments' => $post->comments->transformWith(CommentTransformer::class),
        ];
    }
}
```

Then to transform a single post use

```
$post = Post::find(1);

$array = $post->transform(PostTransformer::class)->show();
```

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~108 days

Total

2

Last Release

3282d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3855298?v=4)[Daniel Harenberg](/maintainers/danhar)[@danhar](https://github.com/danhar)

---

Top Contributors

[![dan-har](https://avatars.githubusercontent.com/u/8581476?v=4)](https://github.com/dan-har "dan-har (16 commits)")

---

Tags

eloquent-transformerlaravelnested-eloquent-modelspresentitpresentit-laraveltransform-collectionsjsonapirestnestedtransformerpresentitpresentit-laraveleloquent-transformerlaravel-model-transformer

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dan-har-presentit-laravel/health.svg)

```
[![Health](https://phpackages.com/badges/dan-har-presentit-laravel/health.svg)](https://phpackages.com/packages/dan-har-presentit-laravel)
```

###  Alternatives

[api-platform/laravel

API Platform support for Laravel

59156.3k11](/packages/api-platform-laravel)

PHPackages © 2026

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