Skip to content

bastien-phi/laravel-view-models

Folders and files

NameName
Last commit message
Last commit date

Latest commit

69c9aed · Sep 7, 2018

History

1 Commit
Sep 7, 2018
Sep 7, 2018
Sep 7, 2018
Sep 7, 2018
Sep 7, 2018
Sep 7, 2018
Sep 7, 2018
Sep 7, 2018
Sep 7, 2018
Sep 7, 2018
Sep 7, 2018
Sep 7, 2018
Sep 7, 2018
Sep 7, 2018
Sep 7, 2018

Repository files navigation

Very short description of the package

Latest Version on Packagist Build Status Quality Score Total Downloads

Installation

You can install the package via composer:

composer require spatie/laravel-viewmodel

Usage

View models are classes used to encapsulate view logic. They help scale large applications with lots of views and view data.

View models are constructed like so:

class PostViewModel extends ViewModel
{
    public function __construct(User $user, Post $post = null)
    {
        $this->user = $user;
        $this->post = $post;
    }
    
    public function post(): Post
    {
        return $this->post ?? new Post();
    }
    
    public function categories(): Collection
    {
        return Category::whereUserAllowed($this->user)->get();
    }
}

They are used in controllers like so:

class PostsController
{
    public function create()
    {
        $viewModel = new PostFormViewModel(
            current_user()
        );
        
        return view('blog.form', $viewModel);
    }
    
    public function edit(Post $post)
    {
        $viewModel = new PostFormViewModel(
            current_user(), 
            $post
        );
    
        return view('blog.form', $viewModel);
    }
}

And allow the views to do this:

<input type="text" value="$post->title" />
<input type="text" value="$post->body" />

<select>
    @foreach ($categories as $category)
        <option value="{{ $category->id }}">{{ $category->name }}</option>
    @endforeach
</select>

All public methods in a view model are automatically exposed to the view. However: methods can also be marked as ignored.

class PostViewModel extends ViewModel
{
    protected $ignored = ['ignored'];

    // …
    
    public function ignored() { /* … */ }
}

All PHP's built in magic methods are also ignored.

View models as responses

It's possible to directly return a view model from a controller. By default, a JSON response with the data is returned.

class PostsController
{
    public function update(Request $request, Post $post)
    {
        // …
        
        return new PostViewModel($post);
    }
}

This approach can be useful when working with AJAX submitted forms.

It's also possible to return a view directly:

class PostsController
{
    public function update(Request $request, Post $post)
    {
        // …
        
        return (new PostViewModel($post))->withView('post.form');
    }
}

Note that when the content-type header of the request is set to JSON, this approach will also return JSON data instead of a rendered view.

Exposing view functions

View models can expose functions which require extra methods.

class PostViewModel extends ViewModel
{
    public function formatDate(Carbon $date): string
    {
        return $date->format('Y-m-d');
    }
}

You can use these functions in the view like so:

{{ $formatDate($post->created_at) }}

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

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

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

Support us

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

License

The MIT License (MIT). Please see License File for more information.

About

View models in Laravel

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%