PHP code example of hemp / presenter

1. Go to this page and download the library: Download hemp/presenter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hemp / presenter example snippets


'providers' => [
    // ...
    Hemp\Presenter\PresenterServiceProvider::class,
],



namespace App\Presenters;

use Hemp\Presenter\Presenter;

class ApiPresenter extends Presenter
{
  public function createdDate()
  {
    return $this->created_at->format('n/j/Y');
  }

  public function getFullNameAttribute()
  {
    return trim($this->first_name . ' ' . $this->last_name);
  }
}

$presentedUser->full_name;

$presentedUser->createdAt();

class ApiPresenter extends Presenter
{
  public $snakeCase = false;
}

Presenter::make($user, ApiPresenter::class)->snakeCase();
Presenter::make($user, ApiPresenter::class)->camelCase();

$user = User::first();
$presentedUser = Presenter::make($user, ApiPresenter::class);

$user = User::first();
$presentedUser = ApiPresenter::make($user);

$user = User::first();
$presentedUser = present($user, ApiPresenter::class);

use Hemp\Presenter\Presentable;

class User extends \Illuminate\Database\Eloquent\Model
{
  use Presentable;
}

$presentedUser = User::first()->present(ApiPresenter::class);

use Hemp\Presenter\Presentable;

class User extends \Illuminate\Database\Eloquent\Model
{
  use Presentable;

  public $defaultPresenter = App\Presenters\ApiPresenter::class;
}

$presentedUser = User::first()->present();

$users = User::all();
$presenter = Presenter::collection($users, ApiPresenter::class);

$users = User::all();
$presenter = ApiPresenter::collection($users);

$presentedUsers = User::all()->present(ApiPresenter::class);



namespace App\Presenters;

use Hemp\Presenter\Presenter;

class ApiPresenter extends Presenter
{
  protected $hidden = ['stripe_private_key'];
}



namespace App\Presenters;

use Hemp\Presenter\Presenter;

class ApiPresenter extends Presenter
{
  protected $visible = ['name', 'email'];
}
sh
php artisan make:presenter ApiPresenter
json
{
  "id": 1,
  "firstName": "David",
  "lastName": "Hemphill",
  "createdAt": "2016-10-14 12:00:00",
  "updatedAt": "2016-12-14 12:00:00",
  "fullName": "David Hemphill"
}