Cryptographic_ICE
Cryptographic_ICE

Reputation: 619

Eloquent column alias

I have a web application that is expecting a column name of “sales_order_number” however the actual column name is uppercase “SALES_ORDER_NUMBER”. I will not be able to change the database so if I can’t do some kind of aliasing I will need to rewrite my webapp to use the new names which will be a pain. Any help would be appreciated.

Upvotes: 1

Views: 808

Answers (1)

Cryptographic_ICE
Cryptographic_ICE

Reputation: 619

The solution was to setup mutators in the model. even though the column name was in snake case I still had to use camel case for the function name. so SALES_ORDER_NUMBER became SalesOrderNumber. here is an example:

protected function getSalesOrderNumberAttribute($value) { return $this->attributes['SALES_ORDER_NUMBER']; }
protected function setSalesOrderNumberAttribute($value) { $this->attributes['SALES_ORDER_NUMBER'] = (is_null($value) ? '' : $value); }

Upvotes: 1

Related Questions