Solved

Laravel 4 - Eloquent column alias

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 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

19 Experts available now in Live!

Get 1:1 Help Now