Download the PHP package artis-auxilium/laravel-lazy-view-models without Composer

On this page you can find all versions of the php package artis-auxilium/laravel-lazy-view-models. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-lazy-view-models

Laravel Lazy View Models

Lazy, reflection-based view models for Laravel Blade templates.

laravel-lazy-view-models lets you write view models where every public method behaves like a property: it's computed lazily, on first access, and that computation happens at most once, no matter how many times or from how many places the value is used.

Why

View models are a great way to keep business logic out of Blade templates, but a naive implementation calls every method up front to build an array of data, even for values the view never touches (conditionally hidden blocks, unused partials, etc.), and recomputes a value every time it's read. This package solves both problems: methods are only invoked when actually needed, and only ever once.

Installation

Requires PHP 8.2+ and Laravel (Illuminate Support/Contracts). Tested against PHP 8.2, 8.3, 8.4, and 8.5 in CI.

Basic usage

Extend the abstract ViewModel class and expose data via public properties or public methods:

The key idea: even though total() and customerName() are declared as methods, everywhere else in the application (in Blade, in other view model methods, from outside the class) they behave as properties$viewModel->total, not $viewModel->total(). That property-style access is what triggers the computation, and only the first access does any work; any later access to the same property, from anywhere, reuses the same result.

Since ViewModel implements Arrayable, pass the instance itself as the view data — Laravel converts it to an array, and every exposed property becomes its own top-level Blade variable:

total and customerName are only computed if and when the template actually reaches these lines.

Callables

If a property's value is itself a callable, invoke it in the template:

Methods with parameters

A method that declares parameters can't be auto-resolved as a property — there's no way to know what arguments to call it with. Instead, it's exposed as directly invokable, and called in Blade with whatever arguments the template wants to pass:

Unlike a parameterless property, this isn't memoized — the underlying method runs on every call, since the result can differ depending on the arguments. #[IsHtml] still applies normally here: the return value of the call is escaped or not exactly as it would be for a parameterless property.

Arrays and iteration

Arrays and traversables work transparently with @foreach:

Values shared between methods

Because every exposed method behaves like a property from anywhere in the class, one method can depend on another simply by reading it as a property — and the underlying computation is still only ever run once, whether it ends up being triggered by the dependent method, by Blade, or by both:

Whether base gets resolved first because the template reads {{ $base }}, or because dependent reads it internally, the second read of base — wherever it comes from — reuses the already-computed result instead of running base() again.

ViewValue: the lazy property wrapper

Every non-ignored public method is wrapped in a ViewValue<T> when the view model is converted to an array (which Laravel does automatically when a view model is passed to a view). ViewValue is what makes property-style access to a method's result work:

Additional helper methods on ViewValue:

Method Description
value(): mixed Force resolution and return the raw underlying value.
empty(): bool empty($resolved).
notEmpty(): bool Inverse of empty().
isset(): bool isset($resolved).

Note: a wrapped method returning a Generator throws an IterableException — generators can only be consumed once, which is incompatible with property-style, potentially-repeated access. Return a LazyCollection (or any other Traversable/array) instead.

Documenting Blade views

Because a view model's variables reach Blade already resolved by name (not as $viewModel->property), the IDE loses the link back to the class that defines them, and can't tell you their real type. A useful convention is to document that link at the top of the Blade file with a @php block:

Attributes

#[Ignore]

Exclude a public property or method from the view model's exposed data — it won't be turned into a property and won't reach the view. Useful for public helper methods but shouldn't be exposed to Blade:

Magic methods (__construct, __toString, __invoke, ...) are always excluded automatically — there's no need to mark them with #[Ignore].

#[IsHtml]

Mark a method's result as pre-escaped HTML. The resolved value is wrapped in an Illuminate\Support\HtmlString, so Blade's {{ }} will output it unescaped:

If the resolved value isn't a scalar or Stringable, the HTML-wrapped result falls back to an empty string.

Exceptions

All exceptions live under ArtisAuxilium\LaravelLazyViewModels\Exception and are thrown when a property's value is used in a way that's incompatible with what it actually resolves to:

Exception Thrown when
StringException Casting to string ((string) $value / {{ $value }}) but the resolved value isn't scalar or Stringable.
ArrayAccessException Using array access ($value['key']) but the resolved value isn't an array or ArrayAccess.
CountableException Calling count($value) but the resolved value isn't an array or Countable.
IterableException Iterating (@foreach) a non-iterable value, or when the resolved value is a Generator.

Note: these exceptions cover misuse of a resolved value's type. For methods with parameters, calling the exposed closure with missing or invalid arguments raises a native PHP error (ArgumentCountError, TypeError, ...), not one of the exceptions above.

Testing & quality

The suite maintains 100% line/method coverage and a 100% Mutation Score Indicator (via Infection) — the CI fails if either drops. A few notes for contributors:

License

MIT.


All versions of laravel-lazy-view-models with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
illuminate/support Version ^8.0|^9.0|^10.0|^11.0|^12.0|^13
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package artis-auxilium/laravel-lazy-view-models contains the following files

Loading the files please wait ...