PHPackages                             engageinteractive/laravel-view-models - 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. [Templating &amp; Views](/categories/templating)
4. /
5. engageinteractive/laravel-view-models

ActiveLibrary[Templating &amp; Views](/categories/templating)

engageinteractive/laravel-view-models
=====================================

Use view models instead of database model in your blade files and JSON responses.

2.0.3(1mo ago)212.9k↓33.3%MITPHPCI failing

Since Mar 29Pushed 1mo ago6 watchersCompare

[ Source](https://github.com/engageinteractive/laravel-view-models)[ Packagist](https://packagist.org/packages/engageinteractive/laravel-view-models)[ RSS](/packages/engageinteractive-laravel-view-models/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (8)Versions (17)Used By (0)

Laravel View Models
===================

[](#laravel-view-models)

[![Build Status](https://camo.githubusercontent.com/ec55960e056ba493b36cdf75aded3332e788b692a603494f26be54b2421d39de/68747470733a2f2f7472617669732d63692e6f72672f656e67616765696e7465726163746976652f6c61726176656c2d766965772d6d6f64656c732e737667)](https://travis-ci.org/engageinteractive/laravel-view-models)[![Total Downloads](https://camo.githubusercontent.com/c739542879d5aeed4de4a83aee0fc4c9c37069fab0cda622b8b6bb960ca75936/68747470733a2f2f706f7365722e707567782e6f72672f656e67616765696e7465726163746976652f6c61726176656c2d766965772d6d6f64656c732f642f746f74616c2e737667)](https://packagist.org/packages/engageinteractive/laravel-view-models)[![Latest Stable Version](https://camo.githubusercontent.com/03d25cb43353b7be891565c735c1941d82206b7fbed7d9cfaf8ae3850563768e/68747470733a2f2f706f7365722e707567782e6f72672f656e67616765696e7465726163746976652f6c61726176656c2d766965772d6d6f64656c732f762f737461626c652e737667)](https://packagist.org/packages/engageinteractive/laravel-view-models)[![License](https://camo.githubusercontent.com/3175a369368b208ec9a6d294f092012dc9aee6f10de680862a10184236fc0c66/68747470733a2f2f706f7365722e707567782e6f72672f656e67616765696e7465726163746976652f6c61726176656c2d766965772d6d6f64656c732f6c6963656e73652e737667)](https://packagist.org/packages/engageinteractive/laravel-view-models)

A straight forward pattern for using mappers/view models instead of database model in your blade files and JSON responses.

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

[](#installation)

```
composer require engageinteractive/laravel-view-models
```

Mappers
-------

[](#mappers)

Create a mapper that can build view models for your Eloquent model:

```
namespace App\Domain\Posts;

use EngageInteractive\LaravelViewModels\Mapper;

use App\Domain\Posts\Post;

class PostShowMapper extends Mapper
{
    /**
     * Map a Post to a basic PHP array.
     *
     * @param \App\Domain\Posts\Post
     * @return array
     */
    public function map(Post $post)
    {
        return $post->only('title', 'author_name');
    }
}
```

Ask for an instance of the Mapper in your controller via the container:

```
namespace App\Domain\Posts;

use App\Domain\Posts\Post;
use App\Domain\Posts\PostMapper;
use App\Http\Controllers\Controller;

class PostController extends Controller
{
    /**
     * Show a Post.
     *
     * @param \App\Domain\Posts\Post
     * @param \App\Domain\Posts\PostMapper
     * @return \Illuminate\Views\View
     */
    public function show(Post $post, PostShowMapper $model)
    {
        return view('post.show', [
            'model' => $mapper->one($post),
        ]),
    }
}
```

View models
-----------

[](#view-models)

Create a view model, to build data to pass into your view:

```
namespace App\Domain\Posts;

use EngageInteractive\LaravelViewModels\ViewModel;
use Illuminate\Support\Str;

class PostViewModel extends ViewModel
{
    protected $post;

    /**
     * Intialise the View ViewModel.
     *
     * @param \App\Domain\Posts\Post
     * @return void
     */
    public function __construct(Post $post): void
    {
        $this->post = $post;
    }

    /**
     * Returns the post title in title-case.
     *
     * @return string
     */
    public function postTitle(): string
    {
        if (!isset($this->post->title)) {
            return 'Untitled';
        }

        return Str::title($this->post->title);
    }
}
```

Pass the ViewModel array into the view:

```
namespace App\Domain\Posts;

use App\Domain\Posts\Post;
use App\Domain\Posts\PostViewModel;
use App\Http\Controllers\Controller;

class PostController extends Controller
{
    /**
     * Show a Post.
     *
     * @param \App\Domain\Posts\Post
     * @return \Illuminate\Views\View
     */
    public function show(Post $post)
    {
        $model = new PostViewModel($post);

        return view('post.show', $model->array()),
    }
}
```

Below is an example of the data passed into the view:

```
[
    'model' => [
        'post_title' => 'This Is The Title',
    ],
]

```

Combining View models and Mappers
---------------------------------

[](#combining-view-models-and-mappers)

First, create a mapper for posts.

```
namespace App\Domain\Posts;

use EngageInteractive\LaravelViewModels\Mapper;

use App\Domain\Posts\Post;

class PostsMapper extends Mapper
{
    /**
     * Map a Post to a basic PHP array.
     *
     * @param \App\Domain\Posts\Post
     * @return array
     */
    public function map(Post $post)
    {
        return $post->only('title', 'author_name');
    }
}
```

Create a ViewModel, and call the mapper.

```
namespace App\Domain\Posts;

use EngageInteractive\LaravelViewModels\ViewModel;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Str;

class PostArchiveViewModel extends ViewModel
{
    protected $posts;

    /**
     * Initialise the View model.
     *
     * @param \Illuminate\Database\Eloquent\Collection
     * @return void
     */
    public function __construct(Collection $posts): void
    {
        $this->posts = $posts;
    }

    /**
     * Returns an array of posts.
     *
     * @return string
     */
    public function posts(): array
    {
        return (new PostsMapper)->all($this->posts);
    }

    /**
     * Returns the application home URI.
     *
     * @return string
     */
    public function HomeUri(): string
    {
        return route('home');
    }
}
```

Pass the view model into the view, with the posts mapped into the required format.

```
namespace App\Domain\Posts;

use App\Domain\Posts\Post;
use App\Domain\Posts\PostArchiveViewModel;
use App\Http\Controllers\Controller;

class PostArchiveController extends Controller
{
    /**
     * Show a Post.
     *
     * @return \Illuminate\Views\View
     */
    public function show()
    {
        $posts = Post::all();
        $model = new PostArchiveViewModel($posts);

        return view('post-archive.show', $model->array()),
    }
}
```

Laravel Compatibility
---------------------

[](#laravel-compatibility)

Works on Laravel 5.5+.

License
-------

[](#license)

Laravel View Models is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).

###  Health Score

53

—

FairBetter than 96% of packages

Maintenance91

Actively maintained with recent releases

Popularity26

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 75% 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 ~200 days

Recently: every ~267 days

Total

14

Last Release

45d ago

Major Versions

1.5.0 → 2.0.02023-06-27

### Community

Maintainers

![](https://www.gravatar.com/avatar/e22aa318e38eb714e24cfaaffbd8ec50c2866cb0c9c17b31425033cee39123db?d=identicon)[engageinteractive](/maintainers/engageinteractive)

---

Top Contributors

[![wilkinsocks](https://avatars.githubusercontent.com/u/2356082?v=4)](https://github.com/wilkinsocks "wilkinsocks (18 commits)")[![JasonStainton](https://avatars.githubusercontent.com/u/27767499?v=4)](https://github.com/JasonStainton "JasonStainton (4 commits)")[![Roberth91](https://avatars.githubusercontent.com/u/6909290?v=4)](https://github.com/Roberth91 "Roberth91 (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/engageinteractive-laravel-view-models/health.svg)

```
[![Health](https://phpackages.com/badges/engageinteractive-laravel-view-models/health.svg)](https://phpackages.com/packages/engageinteractive-laravel-view-models)
```

###  Alternatives

[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135224.7k7](/packages/statamic-rad-pack-runway)[api-platform/laravel

API Platform support for Laravel

58171.6k14](/packages/api-platform-laravel)[ecotone/laravel

Ecotone for Laravel — CQRS, Event Sourcing, Sagas, Durable Workflows, and Outbox on top of Laravel Queue, via PHP attributes.

21318.6k3](/packages/ecotone-laravel)[duncanmcclean/statamic-cargo

Comprehensive e-commerce addon for Statamic. Build bespoke e-commerce sites without the complexity.

3417.0k](/packages/duncanmcclean-statamic-cargo)

PHPackages © 2026

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