Solved

Laravel 4 - Eloquent column alias

Posted on 2014-02-04
3
3,366 Views
Last Modified: 2014-02-10
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.
0
Comment
Question by:HelpDeskGeiger
  • 2
3 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39832651
PHP has the strtoupper() function that can help.  But I don't know how to add a hook or aliasing process to Laravel's SQL queries.

Changing the application might not be as hard as you think.  In most text editors there is a find-in-files function.  You might make a case-sensitive search for sales_order_number to see how many instances of that are in your code.  If the search results look promising, the change might be the fastest way to a solution.

This is often a problem for people moving from Windows to Linux platforms. The rules about case-sensitivity are different.
http://dev.mysql.com/doc/refman/5.0/en/identifier-case-sensitivity.html
0
 

Accepted Solution

by:
HelpDeskGeiger earned 0 total points
ID: 39835676
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); }

Open in new window

0
 

Author Closing Comment

by:HelpDeskGeiger
ID: 39846808
I found the answer in the laravel documentation.
0

Featured Post

Free camera licenses with purchase of My Cloud NAS

Milestone Arcus software is compatible with thousands of industry-leading cameras for added flexibility. Upon installation on your My Cloud NAS, you will receive two (2) camera licenses already enabled in the software. And for a limited time, get additional camera licenses FREE.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now