ruslanashaari
9/24/2017 - 8:43 PM

append a value column into json/array

any custom property that we want to append to any json/array representative from model.

  1. add a public function getter using a camel case of the column to define the column value
  2. set a protected array of the column

p/s: laravel 5.1 above doc ref: https://laravel.com/docs/5.1/eloquent-serialization#appending-values-to-json s/o ref: https://stackoverflow.com/a/28560618/5866081

//in model Reply.php

protected $appends = ['favoritesCount'];


//so when data are being called from this model, element favoritesCount are included in the json/array


//another example from stackoverflow

class User extends Eloquent {
 protected $appends = array('status');

    public function getStatusAttribute() {
      return 'someStatus';
   }
}