Solved

Laravel 4 - Eloquent column alias

Posted on 2014-02-04
3
3,391 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 109

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

785 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