1. Go to this page and download the library: Download halaei/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/ */
halaei / presenter example snippets
use Laracasts\Presenter\Presenter;
class UserPresenter extends Presenter {
public function fullName()
{
return $this->first . ' ' . $this->last;
}
public function accountAge()
{
return $this->created_at->diffForHumans();
}
}
use Halaei\Presenter\PresentableTrait;
class User extends \Eloquent {
use PresentableTrait;
protected $presenter_class = 'UserPresenter';
}
<h1>Hello, {{ $user->present()->fullName }}</h1>
$model->newQuery()->delete(); //delete all the records of table!
function someRepositoryFunction()
{
return $this->model->where('some condition')->firstOrFail()->present();
}
use Halaei\Presenter\PresentableTrait;
class User extends \Eloquent {
use PresentableTrait;
protected $presenter_class = 'UserPresenter';
protected $presenter_callables = ['save']; //to make your presenter a real active record!
}
//client code can do:
/** var UserPresenter $presenter **/
$presenter = $your_user_reopsitory->findById(1);
$presenter->email = '[email protected]';
$presenter->save();
class User extends \Eloquent {
use PresentableTrait;
protected $presenter_class = 'UserPresenter';
protected $presenter_friend = 'YourRepositoryClass';
...
}
class YourRepositoryClass()
{
function save(UserPresenter $user)
{
//you can do
$user->save();
//or equivalently
$user->getModel()->save();
//but that can be done only in this friend class! Outsiders will be blocked by an Exection!
}
}
use Halaei\Presenter\PresenterCollection;
...
function anotherRepositoryFunction()
{
return new PresenterCollection($this->model->where('some condition')->get());
}