Support the ongoing development of Laravel.io →
Eloquent Packages Architecture

Hi, currently it's quite hard to make good interfaces for special column types in Eloquent, like geometry types in postgis, and hstore, json, array, etc. in postgres. Some are easier than others, but will require the user to manually add mutators and/or accessors. Or as in the case with my postgis package(phaza/laravel-postgis), make a trait which overrides many of the Model and Builder methods. But still, the user has to manually add the trait, and define which attributes/columns are geometries. This is a bit error prone, and not very elegant. I wish to discuss if there are other options.

One idea I had, was to query the database for descriptions of all the model tables. If we then had some kind of "accessor/mutator registry" for attributes, it would be easy for packages to register new mutators for given column types, and they would work like a kind of chain/pipe, where the result of one mutator is passed onto the next one until finally the mutated attribute is returned. For a json column, the mutator(s) could return an array or stdObject, and for a postgis geometry the mutator(s) could return a geometry class (Like Point, Polygon, LineString)

Also, currently the way I've implemented the postgis trait, if a user does $model->someAttr, I have to check if the requested attribute is a geometry by comparing the attribute name to a user defined list of attributes to treat like a geometry (much like $casts works today). If the attributes on model kept track of what attributes had mutators, and made sure to call these when an attribute was requested, it would very likely improve performance instead of packages having to loop over attributes to see if they need to be mutated.

Anyone got other ideas or suggestions? I really want some input before I start writing any code to support these ideas.

Last updated 3 years ago.
0

Could be done as a Contract (interface) + Trait to keep things simple.

0

Sign in to participate in this thread!

PHPverse

Your banner here too?

phaza phaza Joined 5 Feb 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.