|
| 1 | +# Very short description of the package |
| 2 | + |
| 3 | +[](https://packagist.org/packages/spatie/laravel-viewmodel) |
| 4 | +[](https://travis-ci.org/spatie/laravel-viewmodel) |
| 5 | +[](https://scrutinizer-ci.com/g/spatie/laravel-viewmodel) |
| 6 | +[](https://packagist.org/packages/spatie/laravel-viewmodel) |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +You can install the package via composer: |
| 11 | + |
| 12 | +```bash |
| 13 | +composer require spatie/laravel-viewmodel |
| 14 | +``` |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +View models are classes used to encapsulate view logic. |
| 19 | +They help scale large applications with lots of views and view data. |
| 20 | + |
| 21 | +View models are constructed like so: |
| 22 | + |
| 23 | +```php |
| 24 | +class PostViewModel extends ViewModel |
| 25 | +{ |
| 26 | + public function __construct(User $user, Post $post = null) |
| 27 | + { |
| 28 | + $this->user = $user; |
| 29 | + $this->post = $post; |
| 30 | + } |
| 31 | + |
| 32 | + public function post(): Post |
| 33 | + { |
| 34 | + return $this->post ?? new Post(); |
| 35 | + } |
| 36 | + |
| 37 | + public function categories(): Collection |
| 38 | + { |
| 39 | + return Category::whereUserAllowed($this->user)->get(); |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +They are used in controllers like so: |
| 45 | + |
| 46 | +```php |
| 47 | +class PostsController |
| 48 | +{ |
| 49 | + public function create() |
| 50 | + { |
| 51 | + $viewModel = new PostFormViewModel( |
| 52 | + current_user() |
| 53 | + ); |
| 54 | + |
| 55 | + return view('blog.form', $viewModel); |
| 56 | + } |
| 57 | + |
| 58 | + public function edit(Post $post) |
| 59 | + { |
| 60 | + $viewModel = new PostFormViewModel( |
| 61 | + current_user(), |
| 62 | + $post |
| 63 | + ); |
| 64 | + |
| 65 | + return view('blog.form', $viewModel); |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +And allow the views to do this: |
| 71 | + |
| 72 | +```html |
| 73 | +<input type="text" value="$post->title" /> |
| 74 | +<input type="text" value="$post->body" /> |
| 75 | + |
| 76 | +<select> |
| 77 | + @foreach ($categories as $category) |
| 78 | + <option value="{{ $category->id }}">{{ $category->name }}</option> |
| 79 | + @endforeach |
| 80 | +</select> |
| 81 | +``` |
| 82 | + |
| 83 | +All public methods in a view model are automatically exposed to the view. |
| 84 | +However: methods can also be marked as ignored. |
| 85 | + |
| 86 | +```php |
| 87 | +class PostViewModel extends ViewModel |
| 88 | +{ |
| 89 | + protected $ignored = ['ignored']; |
| 90 | + |
| 91 | + // … |
| 92 | + |
| 93 | + public function ignored() { /* … */ } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +All PHP's built in magic methods are also ignored. |
| 98 | + |
| 99 | +#### View models as responses |
| 100 | + |
| 101 | +It's possible to directly return a view model from a controller. |
| 102 | +By default, a JSON response with the data is returned. |
| 103 | + |
| 104 | +```php |
| 105 | +class PostsController |
| 106 | +{ |
| 107 | + public function update(Request $request, Post $post) |
| 108 | + { |
| 109 | + // … |
| 110 | + |
| 111 | + return new PostViewModel($post); |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +This approach can be useful when working with AJAX submitted forms. |
| 117 | + |
| 118 | +It's also possible to return a view directly: |
| 119 | + |
| 120 | +```php |
| 121 | +class PostsController |
| 122 | +{ |
| 123 | + public function update(Request $request, Post $post) |
| 124 | + { |
| 125 | + // … |
| 126 | + |
| 127 | + return (new PostViewModel($post))->withView('post.form'); |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +Note that when the content-type header of the request is set to JSON, |
| 133 | +this approach will also return JSON data instead of a rendered view. |
| 134 | + |
| 135 | +#### Exposing view functions |
| 136 | + |
| 137 | +View models can expose functions which require extra methods. |
| 138 | + |
| 139 | +```php |
| 140 | +class PostViewModel extends ViewModel |
| 141 | +{ |
| 142 | + public function formatDate(Carbon $date): string |
| 143 | + { |
| 144 | + return $date->format('Y-m-d'); |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +You can use these functions in the view like so: |
| 150 | + |
| 151 | +```blade |
| 152 | +{{ $formatDate($post->created_at) }} |
| 153 | +``` |
| 154 | + |
| 155 | +### Changelog |
| 156 | + |
| 157 | +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. |
| 158 | + |
| 159 | +## Contributing |
| 160 | + |
| 161 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 162 | + |
| 163 | +### Security |
| 164 | + |
| 165 | +If you discover any security related issues, please email [email protected] instead of using the issue tracker. |
| 166 | + |
| 167 | +## Postcardware |
| 168 | + |
| 169 | +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. |
| 170 | + |
| 171 | +Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium. |
| 172 | + |
| 173 | +We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards). |
| 174 | + |
| 175 | +## Credits |
| 176 | + |
| 177 | +- [Brent Roose](https://github.com/brendt) |
| 178 | +- [All Contributors](../../contributors) |
| 179 | + |
| 180 | +## Support us |
| 181 | + |
| 182 | +Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource). |
| 183 | + |
| 184 | +Does your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/spatie). |
| 185 | +All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff. |
| 186 | + |
| 187 | +## License |
| 188 | + |
| 189 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments