1

I would like to access database model properties in camelCase in my code, but store them as snake_case in my tables (as appears to be standard).

I've read that Eloquent will automatically convert between them on retrieving/saving, but that doesn't appear true.

One poor solution is to manually define the camel case version of each property, but then save() tries to write all properties to the table, creating column not found error.

Is the best solution to just use getters and setters for each property?

Is there any better way? Thank you!

3
  • how it doesn't appear true? share your example with error? Commented Apr 8, 2017 at 5:18
  • Show example, weird problem.. Commented Apr 8, 2017 at 5:19
  • I mean if table has column 'entry_id', and I instantiate a Model object to represent that record, then $model->entryId is undefined Commented Apr 8, 2017 at 5:38

1 Answer 1

4

If I understand your question correctly, this Laravel package should do what you need.

https://github.com/kirkbushell/eloquence

Sign up to request clarification or add additional context in comments.

2 Comments

What's the downvote for? The package I linked to provides camelcase accessors for all model attributes.
thanks that package looks great! Seems to plug this hole.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.