{"id":581,"date":"2016-09-16T17:25:07","date_gmt":"2016-09-16T20:25:07","guid":{"rendered":"http:\/\/www.henriquereis.com\/blog\/?p=581"},"modified":"2016-09-16T17:28:04","modified_gmt":"2016-09-16T20:28:04","slug":"laravel-morphmany-and-morphone-not-handle-the-model-pci-how-to-solve","status":"publish","type":"post","link":"https:\/\/henriquereis.com\/blog\/laravel-morphmany-and-morphone-not-handle-the-model-pci-how-to-solve\/","title":{"rendered":"Laravel: morphMany and morphOne not handle the model pci, how to solve"},"content":{"rendered":"<p>Problem:<br \/>\nWhen I have:<\/p>\n<blockquote><p>\nnamespace Domain\\Client;<\/p>\n<p>use Domain\\Address\\Address;<br \/>\nuse Illuminate\\Database\\Eloquent\\Model;<\/p>\n<p>class Client extends Model {<br \/>\n    public function addresses()<br \/>\n    {<br \/>\n        return $this->morphMany(Address::class, &#8216;owner&#8217;);<br \/>\n    }<br \/>\n}<\/p>\n<p>&#8212;-<br \/>\nnamespace Domain\\Customer;<\/p>\n<p>class Customer extends \\Domain\\Client\\Client<br \/>\n{<br \/>\n}\n<\/p><\/blockquote>\n<p>The &#8220;addresses&#8221; relationship doesn&#8217;t work because the class morphMany is getting (by default) the class name &#8220;Domain\\Customer\\Customer&#8221; and in reality, it must get the &#8220;father class&#8221; which is &#8220;Domain\\Client\\Client&#8221;.<\/p>\n<p>I propose to solve this problem by adding a parameter to inform the &#8220;father class&#8221; in the following way, extends class &#8220;Model of Laravel and add its methods&#8221;:<\/p>\n<blockquote><p>\n    \/**<br \/>\n     * Define a polymorphic one-to-one relationship.<br \/>\n     *<br \/>\n     * @param  string  $related<br \/>\n     * @param  string  $name<br \/>\n     * @param  string  $type<br \/>\n     * @param  string  $id<br \/>\n     * @param  string  $localKey<br \/>\n     * @param  \\Illuminate\\Database\\Eloquent\\Model  $parent<br \/>\n     * @return \\Illuminate\\Database\\Eloquent\\Relations\\MorphOne<br \/>\n     *\/<br \/>\n    public function morphOne($related, $name, $type = null, $id = null, $localKey = null, Model $parent = null)<br \/>\n    {<br \/>\n        $instance = new $related;<\/p>\n<p>        list($type, $id) = $this->getMorphs($name, $type, $id);<\/p>\n<p>        $table = $instance->getTable();<\/p>\n<p>        $localKey = $localKey ?: $this->getKeyName();<\/p>\n<p>        if (is_null($parent)) {<br \/>\n            $parent = $this;<br \/>\n        }<\/p>\n<p>        return new MorphOne($instance->newQuery(), $parent, $table.&#8217;.&#8217;.$type, $table.&#8217;.&#8217;.$id, $localKey);<br \/>\n    }<\/p>\n<p>    public function attributesToArray()<br \/>\n    {<br \/>\n        $attributes = parent::attributesToArray();<br \/>\n        $dates = $this->getCastDates();<br \/>\n        foreach ($dates as $key) {<br \/>\n            if (!array_key_exists($key, $attributes)) {<br \/>\n                continue;<br \/>\n            }<\/p>\n<p>            $attributes[$key] = Carbon::parse($attributes[$key])->format(&#8216;Y-m-d&#8217;);<br \/>\n        }<\/p>\n<\/blockquote>\n<p>Now<br \/>\nway to solve with the proposed amendment would inform it in class father, like this:<\/p>\n<blockquote><p>\nnamespace Domain\\Client;<\/p>\n<p>use Domain\\Address\\Address;<br \/>\nuse You-Model-With-Methods;<\/p>\n<p>class Client extends You-Model-With-Methods {<br \/>\n    public function addresses()<br \/>\n    {<br \/>\n        return $this->morphMany(Address::class, &#8216;owner&#8217;, null, null, null, new Self);<br \/>\n    }<br \/>\n}\n<\/p><\/blockquote>\n<p>It was proposed in the framework Laravel one PR, if accepted will not need to do this, you already have a native solution.<br \/>\n<a href=\"https:\/\/github.com\/laravel\/framework\/pull\/15471\">https:\/\/github.com\/laravel\/framework\/pull\/15471<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: When I have: namespace Domain\\Client; use Domain\\Address\\Address; use Illuminate\\Database\\Eloquent\\Model; class Client extends Model { public function addresses() { return $this->morphMany(Address::class, &#8216;owner&#8217;); } } &#8212;- namespace Domain\\Customer; class Customer extends \\Domain\\Client\\Client { } The &#8220;addresses&#8221; relationship doesn&#8217;t work because the class morphMany is getting (by default) the class name &#8220;Domain\\Customer\\Customer&#8221; and in reality, it must &hellip; <a href=\"https:\/\/henriquereis.com\/blog\/laravel-morphmany-and-morphone-not-handle-the-model-pci-how-to-solve\/\" class=\"more-link\">Continue lendo <span class=\"screen-reader-text\">Laravel: morphMany and morphOne not handle the model pci, how to solve<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/henriquereis.com\/blog\/wp-json\/wp\/v2\/posts\/581"}],"collection":[{"href":"https:\/\/henriquereis.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/henriquereis.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/henriquereis.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/henriquereis.com\/blog\/wp-json\/wp\/v2\/comments?post=581"}],"version-history":[{"count":2,"href":"https:\/\/henriquereis.com\/blog\/wp-json\/wp\/v2\/posts\/581\/revisions"}],"predecessor-version":[{"id":583,"href":"https:\/\/henriquereis.com\/blog\/wp-json\/wp\/v2\/posts\/581\/revisions\/583"}],"wp:attachment":[{"href":"https:\/\/henriquereis.com\/blog\/wp-json\/wp\/v2\/media?parent=581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/henriquereis.com\/blog\/wp-json\/wp\/v2\/categories?post=581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/henriquereis.com\/blog\/wp-json\/wp\/v2\/tags?post=581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}