Laravel Eloquent: Rename fields only in Model Class -
i have table have many fields have bad names , containes underscores "_" want rename them (or give them alias) in class, not in table self, because table used other application don't changes in database.
this example of want:
what have:
$model->foo_bar_wtf_man
what want:
$model->foobarwtfman
i wouldn't call having _
in db names bad, here's easy way:
// basemodel extending eloquent\model, other models extend basemodel public function __get($key) { return (parent::__get($key)) ?: parent::__get(snake_case($key)); }
then can do:
// either $foo->bar_baz_wtf; // or $foo->barbazwtf;
that's generic solution. if want handle 'bad names' well, need accessors each 1 of them.
Comments
Post a Comment