Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

[5.2] Access Eloquent attributes by snake case and camel case #41

Closed
andrewmclagan opened this issue Apr 6, 2016 · 14 comments
Closed

Comments

@andrewmclagan
Copy link

according to issue laravel/framework#316 it is possible to access model attributes via snake_case and camelCase regardless of how they are defined.

Is this correct? Does not seem to be the case as of L5.2

@robclancy
Copy link

This caused issue after issue when it was done. Many versions of this were written and finally support was removed because there was always some way it bugged out or confused developers.

@tomschlick
Copy link

yeah case changing generally seems like a bad idea. IMO it's good that it was removed

@andrewmclagan
Copy link
Author

Although there is the persistent issue:

When using Laravel as a RESTful API serving a javascript (or other) front end such as react or angular. The preference for such languages is camel case. This causes problems with linters and other platform specifics.

Is there no possible way?

@tomschlick
Copy link

@andrewmclagan you could always use something like dingo/api or fractal to format your api responses the way you need them. but by default doing transforms on attribute/variable names seems kind of dangerous in terms of bugs

@andrewmclagan
Copy link
Author

Im doing exactly that and it works fine.

Although we also implemented a middleware to transform incoming requests from camleCase to snake_case. Basically the reverse of the above. So we would get snake_case in and camelCase out without messing up Laravels picky internals.

The problem with that approach is we also validate with FormRequests and they do not go through any HTTP middlewares within the app (for reasons i do not understand), thus skipping our transformer and failing validation e.g.

Cannot find 'first_name' field in the request

Well .. no you cant. Laravel has not run my middleware so its still firstName

@lagbox
Copy link

lagbox commented Apr 15, 2016

@andrewmclagan
Im not sure why you keep thinking the middleware stack gets 'skipped'.
Create a FormRequest and add a dd('in form request') somewhere, anywhere. Now add a test middleware to the route you are going to use that FormRequest in. In this middleware's handle method do a dd('in middleware').
What result do you think you will see?

@tomschlick
Copy link

@lagbox from what i'm seeing, form requests are initialized before the data is sent through middleware.

so even if you modify the request in the middleware when the form request validate() method is used it has the original request info, not the modified data.

@lagbox
Copy link

lagbox commented Apr 15, 2016

Yea that is the issue, that is not the behavior I see currently :-).

If I merge inputs in a middleware, my FormRequest mirrors this ( dd($this->all()) in validate() ). Also in the controller doing dd($formrequest->all()) also mirrors this.

So it would not be possible for the request to not have passed through the middleware, or I would not have these merged inputs available, or the session store on the request, or the user resolver correctly bound to the request, or the csrf middleware checking the token, etc ...

I am on 5.2.29 btw.

@andrewmclagan
Copy link
Author

@tomschlick, yep exactly. From requests do not go through middleware stack.

@lagbox
Copy link

lagbox commented Apr 16, 2016

It doesn't matter if the actual FormRequest is going through the stack. The Request itself goes through the stack.

@andrewmclagan
Copy link
Author

...

@wujunze
Copy link

wujunze commented Aug 13, 2018

Hello
The Laravel version I use is 5.5

It is strange. I have a first_name column in my DB, so I need to use $user->first_name. According to what you said, we can use $user->firstName. But it does not work. Have I forgotten something?

Thanks in advance.
@taylorotwell

@wujunze
Copy link

wujunze commented Aug 13, 2018

I got the answer from Stack Overflow, thank you @taylorotwell

@sisve
Copy link

sisve commented Aug 13, 2018

@wujunze Since you didn't provide the answer... was it $model->getAttribute('first_name')?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants