SPresnac's avatar
Level 11

Transform database field names for all model accesses

Hej šŸ‘‹,

imagine I do have a very old database with "read-only" access and in the several tables are tables with fields that do not match well with eloquent. Example: ARTICLE_-_SUPPLIER_AID. Now, you cannot simple to things like: {{ $model->ARTICLE_-_SUPPLIER_AID }}

My question is: Is there a laravelish default way to tell the model to always rename fields with a mapping? Example:

class mymodel {
  protected $mapping = [
    'ARTICLE_-_SUPPLIER_AID' => 'supplier_aid',
    'otherstrangefield' => 'newname',
    // ....
  ];
}

Maybe there already is a way that I simple to not know....

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To handle the transformation of database field names to more readable attribute names in Laravel, you can use Eloquent's getAttribute and setAttribute methods. This allows you to define a mapping array in your model and then use these methods to translate between the database field names and the attribute names you want to use in your application.

Here's how you can achieve this:

  1. Define a mapping array in your model.
  2. Override the getAttribute and setAttribute methods to handle the translation.

Here's an example implementation:

class MyModel extends \Illuminate\Database\Eloquent\Model
{
    protected $table = 'your_table_name';

    protected $mapping = [
        'ARTICLE_-_SUPPLIER_AID' => 'supplier_aid',
        'otherstrangefield' => 'newname',
        // Add more mappings as needed
    ];

    public function getAttribute($key)
    {
        $key = array_search($key, $this->mapping) ?: $key;
        return parent::getAttribute($key);
    }

    public function setAttribute($key, $value)
    {
        $key = $this->mapping[$key] ?? $key;
        return parent::setAttribute($key, $value);
    }
}

Explanation:

  1. Mapping Array: The $mapping array holds the mapping between the database field names and the attribute names you want to use in your application.

  2. getAttribute Method: This method is overridden to check if the requested attribute name exists in the mapping array. If it does, it translates the attribute name to the corresponding database field name before calling the parent getAttribute method.

  3. setAttribute Method: This method is overridden to check if the attribute name being set exists in the mapping array. If it does, it translates the attribute name to the corresponding database field name before calling the parent setAttribute method.

With this setup, you can now use the more readable attribute names in your application code:

$model = MyModel::find(1);
echo $model->supplier_aid; // Accesses ARTICLE_-_SUPPLIER_AID in the database
$model->newname = 'Some Value'; // Sets the value of otherstrangefield in the database

This approach keeps your application code clean and readable while still working with the legacy database field names.

Please or to participate in this conversation.