-
Notifications
You must be signed in to change notification settings - Fork 28
[5.2] Access Eloquent attributes by snake case and camel case #41
Comments
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. |
yeah case changing generally seems like a bad idea. IMO it's good that it was removed |
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? |
@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 |
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.
Well .. no you cant. Laravel has not run my middleware so its still |
@andrewmclagan |
@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 |
Yea that is the issue, that is not the behavior I see currently :-). If I merge inputs in a middleware, my FormRequest 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. |
@tomschlick, yep exactly. From requests do not go through middleware stack. |
It doesn't matter if the actual FormRequest is going through the stack. The Request itself goes through the stack. |
... |
Hello 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. |
I got the answer from Stack Overflow, thank you @taylorotwell |
@wujunze Since you didn't provide the answer... was it |
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
The text was updated successfully, but these errors were encountered: