Laravel: Returning the namespaced owner of a polymorphic relation

后端 未结 6 1999
刺人心
刺人心 2020-12-02 10:28

I can find a number of discussions regarding this but no clear solution. Here are two links, although I will cover everything in my own question here.

Github Issues<

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 11:19

    Let laravel put it into the db - namespace and all. If you need the short classname for something besides making your database prettier then define an accessor for something like :

    morphOne('App\Photos\Photo', 'imageable');
        }
    
        public function getMorphObjectAttribute()
        {
            return (new \ReflectionClass($this))->getShortName();
        }
    
    }
    

    The reasoning I always come back to in scenarios like this is that Laravel is pretty well tested, and works as expected for the most part. Why fight the framework if you don't have to - particularly if you are simply annoyed by the namespace being in the db. I agree it isn't a great idea to do so, but I also feel that I can spend my time more usefully getting over it and working on domain code.

提交回复
热议问题