Implementing deep and eloquent models with Laravel 5.4

73b0a25ed03f649ec63ded82ce4ac7cb?s=47 nunulk
June 21, 2017

Implementing deep and eloquent models with Laravel 5.4

73b0a25ed03f649ec63ded82ce4ac7cb?s=128

nunulk

June 21, 2017
Tweet

Transcript

  1. 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
  2. 6.

    Model inherits Eloquent\Model class User extends Model
 {
 } //

    retrieving a model
 $user = User::find($id);
 // accessors & mutators
 $user->email

  3. 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

  4. 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

  5. 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
  6. 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
  7. 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
  8. 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();
  9. 15.

    Model as ViewModel <template>
 <div class=“p-user”>
 <span class=“p-user__avater”><img :src=“user.avater”></span>
 <span

    class=“p-user__name”>{{ user.full_name }}</span>
 </div>
 </template>
 <script>
 export default {
 data () {
 return {
 user: {},
 }
 },
 created () {
 window.axios.get(‘/api/user’)[…]
 }
 }
 </script>