How do I include a derived value in my API response

Viewed 24

So let's just say I have a date_of_birth field in my table. I want both date of birth and the derived value of age to be in my API response. my initial thought was to have a user model method return a derived value

my abstract idea is something like this:

<?php

class User extends Authenticatable implements JWTSubject
{
 ...
  protected $show = ['age']


  public function age(){
      // show age function
  }
 ...
}

is this possible if not is there a better way ?

1 Answers

Appending Values To JSON

class User extends Authenticatable implements JWTSubject
{
 ...
 protected $appends = ['age', 'birth_year'];


  public function age(){
      // show age function
  }
 ...
}
Related