PHPackages                             sfneal/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. sfneal/view-models

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

sfneal/view-models
==================

spatie/view-models wrapper with built in response caching.

5.2.0(1y ago)091.6k[1 issues](https://github.com/sfneal/view-models/issues)[3 PRs](https://github.com/sfneal/view-models/pulls)2MITPHPPHP ^8.0|^8.1|^8.2|^8.3CI passing

Since Aug 19Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/sfneal/view-models)[ Packagist](https://packagist.org/packages/sfneal/view-models)[ Docs](https://github.com/sfneal/view-models)[ RSS](/packages/sfneal-view-models/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (10)Versions (38)Used By (2)

View Models
===========

[](#view-models)

[![Packagist PHP support](https://camo.githubusercontent.com/42eb1365ae53c5b7fb7e1cf35cbedce7e0d1ee911a3070183865d3a9fbff647f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f73666e65616c2f766965772d6d6f64656c73)](https://packagist.org/packages/sfneal/view-models)[![Latest Version on Packagist](https://camo.githubusercontent.com/2b1dd0e8109c3c82a30436bb0d4336163500ef1cda4d542a467d216457a57e58/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73666e65616c2f766965772d6d6f64656c732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sfneal/view-models)[![Build Status](https://camo.githubusercontent.com/2c83132c68d21013b2a854b6a065253d66cc90da131e5a543fff71db17f0bc87/68747470733a2f2f7472617669732d63692e636f6d2f73666e65616c2f766965772d6d6f64656c732e7376673f6272616e63683d6d6173746572267374796c653d666c61742d737175617265)](https://travis-ci.com/sfneal/view-models)[![StyleCI](https://camo.githubusercontent.com/c8f89f061eef091db7e996d646d964dd8da081d9b8c05258eed29ebc45585e08/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3238383738323333342f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/288782334?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/30aaab0be58faf966e3dba3790859665a5e0cabe1d9700abee06ece6599b59e4/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f73666e65616c2f766965772d6d6f64656c732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/sfneal/view-models/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/22bfdec9b48742e22839134b03c0d54558a2a039640850bb5f992f8964ca417b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73666e65616c2f766965772d6d6f64656c732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sfneal/view-models)

spatie/view-models wrapper with built in response caching

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

[](#installation)

You can install the package via composer:

```
composer require sfneal/view-models
```

Usage
-----

[](#usage)

A view model is a class where you can put some complex logic for your views. This will make your controllers a bit lighter. You can create a view model by extending the provided Sfneal\\ViewModels\\AbstractViewModel.

```
use Sfneal\ViewModels\ViewModel;

class PostViewModel extends ViewModel
{
    public $indexUrl = null;

    public function __construct(User $user, Post $post = null)
    {
        $this->user = $user;
        $this->post = $post;

        $this->indexUrl = action([PostsController::class, 'index']);
        $this->view = 'your.view';
    }

    public function post(): Post
    {
        return $this->post ?? new Post();
    }

    public function categories(): Collection
    {
        return Category::canBeUsedBy($this->user)->get();
    }
}
```

And used in controllers like so:

```
class PostsController
{
    public function create()
    {
        // Uses caching for fast load times after first request
        return (new PostViewModel(current_user()))->render();
    }

    public function edit(Post $post)
    {
        // Doesn't use caching to avoid need for cache invalidation on changes
        return (new PostViewModel(current_user(), $post))->renderNoCache();
    }
}
```

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

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

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Stephen Neal](https://github.com/sfneal)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

PHP Package Boilerplate
-----------------------

[](#php-package-boilerplate)

This package was generated using the [PHP Package Boilerplate](https://laravelpackageboilerplate.com).

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance60

Regular maintenance activity

Popularity25

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity81

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 96.6% 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 ~46 days

Total

34

Last Release

570d ago

Major Versions

0.4.2 → 1.0.02021-01-27

1.1.3 → 2.0.02021-03-29

2.1.0 → 3.0.02021-04-06

3.1.0 → 4.0.02022-03-01

4.0.0 → 5.0.02024-04-17

PHP version history (5 changes)0.1.0PHP ^7.1

0.2.0PHP ^7.2

0.3.0PHP &gt;=7.1

4.0.0PHP ^7.3|^8.0

5.0.0PHP ^8.0|^8.1|^8.2|^8.3

### Community

Maintainers

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

---

Top Contributors

[![sfneal](https://avatars.githubusercontent.com/u/23200215?v=4)](https://github.com/sfneal "sfneal (140 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (5 commits)")

---

Tags

sfnealview-models

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[nystudio107/craft-twigpack

Twigpack is a bridge between Twig and webpack, with manifest.json &amp; webpack-dev-server HMR support

97341.4k17](/packages/nystudio107-craft-twigpack)[localgovdrupal/localgov_base

The base theme for LocalGov Drupal websites.

13121.5k4](/packages/localgovdrupal-localgov-base)

PHPackages © 2026

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