Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Implementing deep and eloquent models with Laravel 5.4

Implementing deep and eloquent models with Laravel 5.4

nunulk

June 21, 2017
Tweet

More Decks by nunulk

Other Decks in Programming

Transcript

  1. Implementing deep and
    eloquent models with
    Laravel 5.4
    Laravel/Vue ษڧձ #1
    nunulk

    View full-size slide

  2. Who am I?
    $I->am(‘nunulk’)
    ->workAs(‘freelance programmer’)
    ->tackleComplexityOfSoftware()

    ->with([

    ‘Domain-Driven Design’,

    ‘Laravel’

    ]);

    View full-size slide

  3. deep model
    “A deep model provides a lucid expression of the
    primary concerns of the domain experts and their
    most relevant knowledge while it sloughs of the
    superficial aspects of the domain.”

    - Domain-Driven Design

    View full-size slide

  4. eloquent
    “expressing what you mean using clear and
    effective language”

    - Macmillan dictionary

    View full-size slide

  5. Domain
    User

    in
    users table

    View full-size slide

  6. Model inherits Eloquent\Model
    class User extends Model

    {

    }
    // retrieving a model

    $user = User::find($id);

    // accessors & mutators

    $user->email


    View full-size slide

  7. Domain
    Guest

    in
    users table
    Host

    in

    users table
    Pair

    View full-size slide

  8. Model inherits Eloquent\Model
    class Guest extends Model

    {

    protected $table = ‘users’;

    protected static function boot()

    {

    parent::boot();

    static::addGlobalScope(‘guest’, function (Builder $builder) {

    // restrictions here

    });

    }

    }
    // retrieving a model

    $guest = Guest::find($id);

    // accessors & mutators

    $guest->email


    View full-size slide

  9. Model has Eloquent\Model
    class Guest

    {

    protected $user;

    public function __construct(User $user)

    {

    $this->user = $user;

    }

    public function __get($key)

    {

    return $this->user->$key;

    }

    }
    // retrieving a model

    $guest = new Guest(User::find($id));

    // accessors & mutators

    $guest->email


    View full-size slide

  10. Model as ValueObject
    class ElapsedTime

    {

    public function __toString()

    {

    $diff = $this->from->diff($this->to);

    if ($diff->d > 0) {

    return $diff->d . ‘೔લ’;

    }

    […]

    }

    }

    class User extends Model

    {

    public function getLastLoggedInAtAttribute($value)

    {

    return new ElapsedTime($value, Carbon::now()); // N೔લ, N࣌ؒલ, N෼લ, ͨͬͨࠓ

    }

    }
    // accessors & mutators

    $user->last_logged_in_at // instance of ElapsedTime

    View full-size slide

  11. Model as Enum
    class UserRole extends Enum

    {

    const HOST = 1;

    const GUEST = 2;

    static private $list = [

    self::HOST => ‘ϗετ’,

    self::GUEST => ‘ήετ’

    ];

    }

    class User extends Model

    {

    public function getRoleAttribute($value)

    {

    return new UserRole($value);

    }

    }
    // accessors & mutators

    $user->role // instance of UserRole

    View full-size slide

  12. Application
    Laravel Vue
    Data

    View full-size slide

  13. Model as ViewModel with Fluent
    abstract class ViewModel extends Fluent

    {

    abstract public function render();

    }

    class ConcreteViewModel extends ViewModel

    {

    public function render()

    {

    return […]

    }

    }
    $vm = new ConcreteViewModel([‘foo’ => $foo]);

    $vm->foo // attribute

    View full-size slide

  14. Model as ViewModel
    class UserView extends ViewModel

    {

    public function render()

    {

    $additionals = [‘full_name’];

    return $this->user->append($additionals);

    }

    }

    class User extends Model

    {

    public function getFullNameAttribute()

    {

    return $this->family_name . ’ ’ . $this->given_name;

    }

    }
    // like DTO

    return (new UserView([‘user’ => $user]))->render();

    View full-size slide

  15. Model as ViewModel



    {{ user.full_name }}



    
<br/>export default {
<br/>data () {
<br/>return {
<br/>user: {},
<br/>}
<br/>},
<br/>created () {
<br/>window.axios.get(‘/api/user’)[…]
<br/>}
<br/>}
<br/>

    View full-size slide

  16. deep and eloquent model
    •Eloquent\Model
    •ValueObject
    •Enum
    •ViewModel (Fluent)
    •Vue.js
    and more…

    View full-size slide