{"id":7893,"date":"2015-11-06T16:35:18","date_gmt":"2015-11-06T16:35:18","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/06\/add-a-custom-attribute-to-a-laravel-eloquent-model-on-load-open-source-projects-laravel-laravel\/"},"modified":"2022-08-30T15:45:32","modified_gmt":"2022-08-30T15:45:32","slug":"add-a-custom-attribute-to-a-laravel-eloquent-model-on-load-open-source-projects-laravel-laravel","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/06\/add-a-custom-attribute-to-a-laravel-eloquent-model-on-load-open-source-projects-laravel-laravel\/","title":{"rendered":"Add a custom attribute to a Laravel \/ Eloquent model on load?-open source projects laravel\/laravel"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/NjTlK.png?s=128&amp;g=1\" \/> <strong>coatesap<\/strong><\/p>\n<p>The problem is caused by the fact that the <code>Model<\/code>&#8216;s <code>toArray()<\/code> method ignores any accessors which do not directly relate to a column in the underlying table.<\/p>\n<p>As Taylor Otwell mentioned here, &#8220;This is intentional and for performance reasons.&#8221;<\/p>\n<p>The best solution that I&#8217;ve found so far is to override the <code>toArray()<\/code> method and either explicity set the attribute:<\/p>\n<pre><code>class Book extends Eloquent {\n\n    protected $table = 'books';\n\n    public function toArray()\n    {\n        $array = parent::toArray();\n        $array['upper'] = $this-&gt;upper;\n        return $array;\n    }\n\n    public function getUpperAttribute()\n    {\n        return strtoupper($this-&gt;title);    \n    }\n\n}\n<\/code><\/pre>\n<p>or, if you have lots of custom accessors, loop through them all and apply them:<\/p>\n<pre><code>class Book extends Eloquent {\n\n    protected $table = 'books';\n\n    public function toArray()\n    {\n        $array = parent::toArray();\n        foreach ($this-&gt;getMutatedAttributes() as $key)\n        {\n            if ( ! array_key_exists($key, $array)) {\n                $array[$key] = $this-&gt;{$key};   \n            }\n        }\n        return $array;\n    }\n\n    public function getUpperAttribute()\n    {\n        return strtoupper($this-&gt;title);    \n    }\n\n}\n<\/code><\/pre>\n<p><strong>Update October 2013:<\/strong> As per trm42&#8217;s answer, as of Laravel v4.08, there is now an easier way to achieve this:<\/p>\n<pre><code>class Book extends Eloquent {\n\n    protected $table = 'books';\n    protected $appends = array('upper');\n\n    public function getUpperAttribute()\n    {\n        return strtoupper($this-&gt;title);    \n    }\n}\n<\/code><\/pre>\n<p>Any attributes listed in the $appends property will automatically be included in the array or JSON form of the model, provided that you&#8217;ve added the appropriate accessor.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>coatesap The problem is caused by the fact that the Model&#8216;s toArray() method ignores any accessors which do not directly relate to a column in the underlying table. As Taylor Otwell mentioned here, &#8220;This is intentional and for performance reasons.&#8221; The best solution that I&#8217;ve found so far is to override the toArray() method and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,1],"tags":[],"class_list":["post-7893","post","type-post","status-publish","format-standard","hentry","category-laravel","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7893"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7893\/revisions"}],"predecessor-version":[{"id":8822,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7893\/revisions\/8822"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}