Giter Club home page Giter Club logo

presenter's Introduction

Presenter for Laravel

Adds another place to put logic for models. Instead of cluttering up models with logic to display/format data, add them to a presenter!

This is a rewrite of laracasts/Presenter with some added functionality, commands, etc.

Install

Via Composer

$ composer require taylornetwork/presenter

Setup

Add the service provider to the providers array in config/app.php

'providers' => [

    TaylorNetwork\Presenter\PresenterServiceProvider::class,

],

Publish Config

$ php artisan vendor:publish

This will add config/presenter.php where you can define the namespace you want your presenter classes to be stored.

Usage

Create a presenter using the artisan command, for example a presenter for the User model.

$ php artisan make:presenter UserPresenter

This will create a presenter class you can add logic to that you don't want in the model or the view. Model attributes are accessible via $this->model

use TaylorNetwork\Presenter\Presenter;

class UserPresenter extends Presenter
{
    /**
     * Get the user's full name
     *
     * @return string
     */
    public function fullName()
    {
        return $this->model->first_name . ' ' . $this->model->last_name;
    }

    /**
     * Get the time since the user signed up
     *
     * @return string
     */
    public function userSince()
    {
        return $this->model->created_at->diffForHumans();
    }
}

You will need to add the presentable trait and a $presenter property to your model

use TaylorNetwork\Presenter\Presentable;
use App\Presenters\UserPresenter;

class User extends Model
{
    use Presentable;

    /**
     * Presenter Class
     *
     * @var string
     */
    protected $presenter = UserPresenter::class;

    // Code
}

You can access the presenter with present()

<h1>{{ $user->present()->fullName }}, you signed up {{ $user->present()->userSince }}</h1>

Credits

License

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

presenter's People

Contributors

itssamtaylor avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.