Decorate Your Models and Write Clean/Reusable Code with Presenters.

Overview

Laravel Presenter

The Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status

A clean way to present your model attributes without putting them in the wrong file.

Installation

You can install the package via composer:

composer require coderflexx/laravel-presenter

You can publish the config file with:

php artisan vendor:publish --provider="Coderflex\LaravelPresenter\LaravelPresenterServiceProvider"

This is the contents of the published config file:

return [

    /*
    |--------------------------------------------------------------------------
    | Presenter Namespace
    |--------------------------------------------------------------------------
    |
    | This value informs LaravelPresenter which namespace you will be 
    | selecting to store your presenters by default.
    | If this value equals to null, "App\Presenter" will be used 
    | by default.
    |
    */

    'presenter_namespace'   => 'App\\Presenters',
];

Usage

The implementation of this package is so simple, all what you need to do is the following:

Model Implementation

  • Implement CanPresent Interface
  • Use UsesPresenters Trait
use Coderflex\LaravelPresenter\Concerns\CanPresent;
use Coderflex\LaravelPresenter\Concerns\UsesPresenters;
// ...

class User extends Authenticatable implements CanPresent
{
    use UsesPresenters;

    // ...
}

Create New Model Presenter class

This package gives you an easy way to generate new Presenter class, all you need to do is to use presenter:make command.

php artisan presenter:make UserPresneter

UserPresenter in our case, leaves by default in App\Presenters.

This is the contents of the UserPresenter file:

<?php

namespace App\Presenters;

use Coderflex\LaravelPresenter\Presenter;

class UserPresenter extends Presenter
{
    // 
}

If you want to change the directory, you have two options.

First options is to set the full namespace while you're creating the presenter class

php artisna presneter:make App\Models\Presenter\UserPresenter

Or change presenter_namespace from config/laravel-presenter file.

return [
    ...

    'presenter_namespace'   => 'App\\Presenters',

    ...
];

Using the Presenter Generated Class

After you create the presenter class, you need to register it on the Model by adding the $presenters protected property:

use App\Presenters\UserPresenter;
use Coderflex\LaravelPresenter\Concerns\CanPresent;
use Coderflex\LaravelPresenter\Concerns\UsesPresenters;
// ...

class User extends Authenticatable implements CanPresent
{
    use UsesPresenters;

    protected $presenters = [
        'default' => UserPresenter,
    ];
}

By default, the type of your presenter class is default, but you can use as many of presenters you want, just by identifying the type in $presenters property.

Example

Now, after we generated the presenter class, and we implemented it successfully in our model, we can use it like so:

In your UserPresenter class or any presenter class you generated.

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

We add a new method to present the fullName.

In your blade or any place you want, you can do:

$user->present()->fullName

Your application will show the full name from the method you added.

Adding Another Presenter Type:

Like I said above, by default the type will be deafult but, you can add more types as you need.

Here is an example:

use App\Presenters\UserPresenter;
use Coderflex\LaravelPresenter\Concerns\CanPresent;
use Coderflex\LaravelPresenter\Concerns\UsesPresenters;
// ...

class User extends Authenticatable implements CanPresent
{
    use UsesPresenters;

    protected $presenters = [
        'default' => UserPresenter,
        'setting' => UserSettingPresenter,
    ];
}

Generate new UserSettingPresenter

php artisan presenter:make UserSettingPresenter

Add anything to UserSettingPresenter method

...
class UserSettingPresenter extends Presenter
{
    public function lang()
    {
        return $this->model->settings->defaultLang;
    }
}
...

Finally, set setting as a type:

$user->present('setting')->lang;

By that, you can split your logic and make your code base even cleaner.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

Comments
Releases(v1.2.2)
  • v1.2.2(Dec 18, 2022)

    What's Changed

    • Bump dependabot/fetch-metadata from 1.3.4 to 1.3.5 by @dependabot in https://github.com/coderflexx/laravel-presenter/pull/11
    • Bump ramsey/composer-install from 1 to 2 by @dependabot in https://github.com/coderflexx/laravel-presenter/pull/12
    • GitHub Actions Badges by @ousid in https://github.com/coderflexx/laravel-presenter/pull/13

    Full Changelog: https://github.com/coderflexx/laravel-presenter/compare/v1.2.1...v1.2.2

    Source code(tar.gz)
    Source code(zip)
  • v1.2.1(Oct 22, 2022)

    What's Changed

    • Bump dependabot/fetch-metadata from 1.3.3 to 1.3.4 by @dependabot in https://github.com/coderflexx/laravel-presenter/pull/8
    • update README.md by @ousid in https://github.com/coderflexx/laravel-presenter/pull/10
    • Bump actions/checkout from 2 to 3.1.0 by @dependabot in https://github.com/coderflexx/laravel-presenter/pull/9

    New Contributors

    • @ousid made their first contribution in https://github.com/coderflexx/laravel-presenter/pull/10

    Full Changelog: https://github.com/coderflexx/laravel-presenter/compare/v1.2.0...v1.2.1

    Source code(tar.gz)
    Source code(zip)
  • v1.2.0(Aug 14, 2022)

    What's Changed

    • Change Command Name and fix Typo by @syntafin in https://github.com/coderflexx/laravel-presenter/pull/6

    New Contributors

    • @syntafin made their first contribution in https://github.com/coderflexx/laravel-presenter/pull/6

    Full Changelog: https://github.com/coderflexx/laravel-presenter/compare/v1.1.1...v1.2.0

    Source code(tar.gz)
    Source code(zip)
  • v1.1.1(Feb 13, 2022)

  • v1.1.0(Feb 12, 2022)

  • v1.0.5(Feb 12, 2022)

  • v1.0.4(Feb 12, 2022)

  • v1.0.3(Feb 12, 2022)

  • v1.0.2(Feb 12, 2022)

  • v1.0.1(Feb 11, 2022)

  • v1.0.0(Feb 11, 2022)

Owner
Coderflex
Full Stack web development Company
Coderflex
A system for auto-decorating models with presenters

Laravel Auto Presenter 7 This package automatically decorates objects bound to views during the view render process. Features Automatically decorate o

Laravel Auto Presenter 754 Dec 21, 2022
An opinionated support package for Laravel, that provides flexible and reusable helper methods and traits for commonly used functionality.

Support An opinionated support package for Laravel, that provides flexible and reusable helper methods and traits for commonly used functionality. Ins

Ian Olson 3 Apr 14, 2021
A collection of reusable components for Filament.

A collection of reusable components for Filament. This package is a collection of handy components for you to use in all your Filament projects. It pr

Ralph J. Smit 35 Nov 16, 2022
Jumpstart your web development journey with the HALT Stack Starter Kit, a one-command solution for creating dynamic, scalable, and clean web applications.

Welcome to the HALT Stack Starter Kit! This kit is designed to help you kickstart your web development projects using the HALT Stack, a powerful combi

HALT Stack 6 Jun 7, 2023
A Laravel Code Generator based on your Models using Blade Template Engine

Laravel Code Generator is a PHP Laravel Package that uses Blade template engine to generate code for you. The difference between other code generators

Victor Yoalli 59 Dec 5, 2022
Testbench Component is the de-facto package that has been designed to help you write tests for your Laravel package

Laravel Testing Helper for Packages Development Testbench Component is the de-facto package that has been designed to help you write tests for your La

Orchestra Platform 1.9k Dec 29, 2022
Clean up and prevent empty meta from being saved for Job, Company, or Resume listings in database

=== Empty Meta Cleanup for WP Job Manager === Contributors: tripflex Tags: wp job manager, meta, cleanup, wpjobmanager Requires at least: 5.2 Tested u

Myles McNamara 3 Feb 7, 2022
Fullstack komponents to write Forms, Queries and Menus in Laravel

kompo.io • Documentation • Demos • Twitter kompo/kompo kompo/kompo is a library of Fullstack Komponents to help you write forms, queries and menus in

Bass El Hachem 107 Dec 5, 2022
Adds a way to write php and run it directly in Laravels' Artisan Tinker.

Adds a way to write php in PhpStorm/IDEA and run it directly as if through laravel artisan tinker - allowing you to quickly run a piece of code with a

Robbin 120 Jan 2, 2023
This is a solution implementation for the coderbyte question, CLEAN GET REQUEST RESULT.

This is a solution implementation for the coderbyte question, CLEAN GET REQUEST RESULT. Two solutions are proposed, the first is a brute force approach while the other is an improved time complexity solution.

null 3 May 23, 2022
Durable workflow engine that allows users to write long running persistent distributed workflows in PHP powered by Laravel queues

Durable workflow engine that allows users to write long running persistent distributed workflows (orchestrations) in PHP powered by Laravel queues. Inspired by Temporal and Azure Durable Functions.

null 268 Dec 27, 2022
Easily add all the 58 Algerian Wilayas and its Dairas to your cool Laravel project (Migrations, Seeders and Models).

Laravel-Algereography Laravel-Algereography allows you to add Migrations, Seeders and Models of Algerian Wilayas and Dairas to your existing or new co

Hocine Saad 48 Nov 25, 2022
Use auto generated UUID slugs to identify and retrieve your Eloquent models.

Laravel Eloquent UUID slug Summary About Features Requirements Installation Examples Compatibility table Alternatives Tests About By default, when get

Khalyomede 25 Dec 14, 2022
A Laravel package making a diagram of your models, relations and the ability to build them with it

Laravel Schematics This package allows you to make multiple diagrams of your Eloquent models and their relations. It will help building them providing

Maarten Tolhuijs 1.4k Dec 31, 2022
Easy creation of slugs for your Eloquent models in Laravel

Eloquent-Sluggable Easy creation of slugs for your Eloquent models in Laravel. NOTE: These instructions are for the latest version of Laravel. If you

Colin Viebrock 3.6k Dec 30, 2022
The package lets you generate TypeScript interfaces from your Laravel models.

Laravel TypeScript The package lets you generate TypeScript interfaces from your Laravel models. Introduction Say you have a model which has several p

Boris Lepikhin 296 Dec 24, 2022
Generate trends for your models. Easily generate charts or reports.

Laravel Trend Generate trends for your models. Easily generate charts or reports. Support us Like our work? You can support us by purchasing one of ou

Flowframe 139 Dec 27, 2022
A simple drop-in solution for providing UUID support for the IDs of your Eloquent models.

Introduction A simple drop-in solution for providing UUID support for the IDs of your Eloquent models. Both v1 and v4 IDs are supported out of the box

GoldSpec Digital 501 Jan 4, 2023
Kalibrant - a package that provides a simple way to manage your models settings

Introduction For your laravel 9.x applications, Kalibrant is a package that provides a simple way to manage your models settings. It is a simple way t

Starfolk 3 Jun 18, 2022