2017 © Pedro Peláez
 

library laravel-view-model

View Models for Laravel 5.x

image

timmcleod/laravel-view-model

View Models for Laravel 5.x

  • Sunday, June 12, 2016
  • by timmcleod
  • Repository
  • 1 Watchers
  • 3 Stars
  • 960 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 5 % Grown

The README.md

View Models for Laravel

Clean up messy views using View Models

This package can be used to help you encapsulate, validate, and manipulate the data that is required by each of the views in your Laravel 5.x applications., (*1)

Purpose and Overview

As the views in our applications grow more complex, they tend to require more data that needs to be presented in varying ways. As our view dependencies increase and the view files become more complex, it can be increasingly difficult to keep track of the data that is required by the view., (*2)

View Models help by:, (*3)

  • Validating the data that is injected into the view to verify that the data is exactly what the view expects.
  • Facilitating the bundling of data with the methods required to manipulate the data in the context of the view.
  • Reducing the chance that the "global" variables in your views will be inadvertently overwritten within your views.
  • Providing a way to see what data is required by the view at-a-glance.

Installation

Require this package with composer:, (*4)

composer require timmcleod/laravel-view-model

Then, register the following service providers in your app config (project/config/app.php) by adding them to the providers array:, (*5)

'providers' => [
    ...
    TimMcLeod\InstanceValidator\InstanceValidatorServiceProvider::class,
    TimMcLeod\ViewModel\ViewModelServiceProvider::class,
]

Getting Started

After you have registered the ViewModelServiceProvider, a new Artisan command will be available to generate your view models. To create a new view model, use the make:view-model Artisan command:, (*6)

php artisan make:view-model EditProfileViewModel

This command will place a new EditProfileViewModel class within your app/Http/ViewModels directory. If the directory doesn't exist, it will be created for you., (*7)

<?php

namespace App\Http\ViewModels;

use TimMcLeod\ViewModel\BaseViewModel;

class EditProfileViewModel extends BaseViewModel
{
    /** @var array */
    protected $rules = [];
}

If you prefer to subgroup your view models, you can generate them this way:, (*8)

php artisan make:view-model User/EditProfileViewModel

This command will place a new EditProfileViewModel class within your app/Http/ViewModels/User directory., (*9)

Basic Usage

To use view models, just create a new instance of a view model and pass the instance into the view., (*10)

$vm = new EditProfileViewModel([
    'timezones' => Timezone::all(),
    'states'    => State::all(),
    'cities'    => City::all(),
    'user'      => Auth::user()
]);

return view('user.profile')->with('vm', $vm);

When the new view model is instantiated, the data is validated using the rules defined in your view model class. You can use any of the available validation rules defined in Laravel documentation., (*11)

Three additional rules are also available: instance_of, collection_of, and paginator_of., (*12)

protected $rules = [
    'timezones' => 'required|collection_of:App\\Timezone',
    'states'    => 'required|collection_of:App\\State',
    'cities'    => 'present|collection_of:App\\City',
    'user'      => 'required|instance_of:App\\User',
];

If any of the data coming from the controller fails validation, an exception is thrown., (*13)

Within your views, you can access the data in your view model like this: $vm->timezones., (*14)

You can also add your own methods to your view model to keep your view-specific logic bundled nicely with the data to keep your views clean., (*15)

// Inside of EditProfileViewModel class:

/**
 * Returns true if the user's timezone is the same as the given timezone.
 *
 * @param Timezone $timezone
 * @return bool
 */
public function isSelectedTimezone(Timezone $timezone)
{
    return $this->user->timezone === $timezone->code;
}


<select id="timezone" name="timezone">
    @foreach($vm->timezones as $timezone)
        <option value="{{$timezone->code}}" {{$vm->isSelectedTimezone($timezone) ? 'selected' : ''}}>{{$timezone->label}}</option>
    @endforeach
</select>

Or maybe you want to create a list of checkboxes for cities in 2 separate columns in this particular view. You could do something like this:, (*16)

// Inside of EditProfileViewModel class:

/**
 * Returns the first half of cities from the cities array.
 *
 * @return array
 */
public function citiesCol1()
{
    $len = count($this->cities);
    return $this->cities->slice(0, round($len / 2));
}

/**
 * Returns the second half of cities from the cities array.
 *
 * @return array
 */
public function citiesCol2()
{
    $len = count($this->cities);
    return $this->cities->slice(round($len / 2));
}



@foreach($vm->citiesCol1() as $city)
    <label><input name="cities[]" type="checkbox" value="{{$city->id}}">{{"$city->name, $city->state_code"}}</label>
@endforeach


@foreach($vm->citiesCol2() as $city)
    <label><input name="cities[]" type="checkbox" value="{{$city->id}}"> {{"$city->name, $city->state_code"}}</label>
@endforeach

As you can see, View Models allow us to bundle the view data with the methods required to manipulate the data in the context of the view., (*17)

License

View Models for Laravel is open-sourced software licensed under the MIT license., (*18)

Laravel is a trademark of Taylor Otwell., (*19)

The Versions

12/06 2016

dev-master

9999999-dev https://github.com/timmcleod/laravel-view-model

View Models for Laravel 5.x

  Sources   Download

MIT

The Requires

 

laravel model view

12/06 2016

v1.0.4

1.0.4.0 https://github.com/timmcleod/laravel-view-model

View Models for Laravel 5.x

  Sources   Download

MIT

The Requires

 

laravel model view

12/06 2016

v1.0.3

1.0.3.0 https://github.com/timmcleod/laravel-view-model

View Models for Laravel 5.x

  Sources   Download

MIT

The Requires

 

laravel model view

26/05 2016

v1.0.2

1.0.2.0 https://github.com/timmcleod/laravel-view-model

View Models for Laravel 5.x

  Sources   Download

MIT

The Requires

 

laravel model view

21/05 2016

v1.0.1

1.0.1.0 https://github.com/timmcleod/laravel-view-model

View Models for Laravel 5.x

  Sources   Download

MIT

The Requires

 

laravel model view

21/05 2016

v1.0.0

1.0.0.0 https://github.com/timmcleod/laravel-view-model

View Models for Laravel 5.x

  Sources   Download

MIT

The Requires

 

laravel model view