Git Product home page Git Product logo

presenter's Introduction

Laravel Presenter

Latest Stable Version Total Downloads Latest Unstable Version License PHPUnit Test

Presenter is a design pattern for Laravel which is used to modify the data that comes from your model to your views.
It causes the data to be displayed in a way understandable to humans.

Installation

Laravel (5.x|6.x|7.x|8.x|9.x|10.x)

Execute the following command to get the latest version of the package:

composer require guilhermegonzaga/presenter

Usage

The first step is to store your presenters somewhere - anywhere. These will be simple objects that do nothing more than format data, as required.
Note that your presenter class must extend Laracodes\Presenter\Presenter:

namespace App\Presenters;

use Laracodes\Presenter\Presenter;

class UserPresenter extends Presenter
{
    public function fullName()
    {
        return $this->model->first_name . ' ' . $this->model->last_name;
    }
    
    public function accountAge()
    {
        return $this->model->created_at->diffForHumans();
    }

    ...
}

Next, on your model, pull in the Laracodes\Presenter\Traits\Presentable trait, which will automatically instantiate your presenter class:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Laracodes\Presenter\Traits\Presentable;

class User extends Model
{
    use Presentable;
    
    protected $presenter = 'App\Presenters\UserPresenter';

    ...
}

Done, now you can call it in your views:

<h1>Hello, {{ $user->present()->fullName }}</h1>
<h1>Hello, {{ $user->present()->full_name }}</h1> // automatically convert to camelCase

Notice how the call to the present() method (which will return your new or cached presenter object) also provides the benefit of making it perfectly clear where you must go, should you need to modify how a full name is displayed on the page.

Notices

When you call a method that does not exist in its class presenter, this package will automatically call the property in the model with conversion to snake_case.

Ex:

// automatically calls the property in the model
<h1>Hello, {{ $user->present()->firstName }}</h1> // automatically convert to snake_case
<h1>Hello, {{ $user->present()->first_name }}</h1>

Credits

This package is largely inspired by this great package by @laracasts.

presenter's People

Contributors

guilhermegonzaga avatar maxgiting avatar jaybizzle avatar conroyp 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.