Skip to content
No description, website, or topics provided.
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config
src
stubs
tests
.gitignore
composer.json
composer.lock
license.md
phpunit.xml
readme.md

readme.md

ViewModel

Installation

Via Composer

$ composer require jaytyrrell13/view-model

If you would like to publish the config file, you can run this command.

php artisan vendor:publish --provider="JayTyrrell13\ViewModel\ViewModelServiceProvider" --tag="config"

Usage

A view model is a class where you can put some presentation data that you don't want in your controller or on your model. You can add the JayTyrrell13\ViewModel\HasViewModel trait to your model like this.

namespace App;

use Illuminate\Database\Eloquent\Model;
use JayTyrrell13\ViewModel\HasViewModel;

class Post extends Model
{
    use HasViewModel;

    //...
}
namespace App\ViewModels;

use App\Post;

class PostViewModel
{
    protected $post;

    public function __construct(Post $post)
    {
        $this->post = $post;
    }

    public function shortTitle()
    {
        return substr($this->post->title, 0, 50);
    }
}

In your controller, just pass the model to the view.

namespace App\Http\Controllers;

class PostsController extends Controller
{
    public function index()
    {
        $posts = Post::all();

        return view('posts.index', compact('posts'));
    }
}

Then in your view, you can reference any public property or public method from the view model.

@foreach($posts as $post)
    <h2>{{ $post->shortTitle() }}</h2>

    <p>{{ $post->description }}</p>
@endforeach

There is an artisan command included that will generate the view model for you.

If you run php artisan make:view-model PostViewModel the view model class will look like this.

<?php

namespace App\ViewModels;

class FooViewModel
{
    //
}

If you run php artisan make:view-model PostViewModel --model=Post the view model class will have the model injected into the constructor like this.

<?php

namespace App\ViewModels;

use App\Post;

class PostViewModel
{
    protected $post;

    public function __construct(Post $post)
    {
        $this->post = $post;
    }
}

If you add the --test parameter, you will get a test class like this.

<?php

namespace Tests\Feature\ViewModels;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class PostViewModelTest extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function testExample()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

Security

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

License

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

You can’t perform that action at this time.