artisanry/eloquent-presenter

Model Presenters for Laravel

4.1.0 2019-06-04 07:19 UTC

README

Build Status PHP from Packagist Latest Version License

Installation

Require this package, with Composer, in the root directory of your project.

$ composer require artisanry/eloquent-presenter

Usage

Presenter

use Artisanry\Presenter\BasePresenter;

class CommentPresenter extends BasePresenter
{
    public function getRoutePrefix()
    {
        return 'users.posts.comments';
    }

    public function getRouteParameters()
    {
        // The ID of the model will be automatically attached to this array at the end
        // [
        //     $model->post->user->id,
        //     $model->post->id,
        //     $model->id // "id" is what is specified in $this->getRouteKeyName()
        // ];

        return ['post.user.id', 'post.id'];
    }
}

Model

use Artisanry\Presenter\HasViewPresenterTrait;
use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    use HasViewPresenterTrait;

    /**
     * Get the view presenter for the model.
     *
     * @return string
     */
    protected function getPresenter()
    {
        return UserPresenter::class;
    }
}

Testing

$ phpunit

Security

If you discover a security vulnerability within this package, please send an e-mail to hello@basecode.sh. All security vulnerabilities will be promptly addressed.

Credits

This project exists thanks to all the people who contribute.

License

Mozilla Public License Version 2.0 (MPL-2.0).