camelCaseD camelCaseD - 1 year ago 97
PHP Question

Laravel 4: Eloquent How to get property of model if column name contains a dash?

I have the following in my root route:

$user = User::all();
return $user->column-one;

Which returns the exception Use of undefined constant one - assumed 'one' even though I do have a column called
from my users table. So how do I get
from my model?

Answer Source

After digging through the source code for the eloquent model I found the magic method __get and learned that it was just a wrapper for the public function getAttribute which takes a string thus I'm now able to retrieve the column via $user->getAttribute('column-one');.

See @Alexandre Butynski's comment below for a better solution than the one I used.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download