-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.5] change how request only works and remove intersect #18695
Conversation
Don't have any relevant feedback (other than 'I love it') but I wanted to mention how much I love all those red lines in the commit. 😍😍😍 |
I think this makes total sense. Brings consistency and, what I feel, is what you would expect from the When I build an API that supports Awesome |
I didn't know Request did that. I prefer the behavior that this PR brings to the framework. |
The old behavior can be useful when dealing with checkboxes since an unchecked checkbox will be completely absent from submitted form data (rather than being present and set to false or null, etc.). The old behavior lets you specify all the fields you want to update and ensures they're updated even if they are missing from the form post data. I like the consistency that this change brings, but it might make certain situations a little more awkward. Is there another method that replicates the old behavior? |
Yes please, this is definitely the most logical way the only method should work. The way it works now (before this PR) prevents me from using it especially in handling PATCH requests. When you think about it it doesn't make sense that a method called 'only' will create new values, it should do no more than filter existing values. |
@misenhower one suggestion I have mentioned internally is to have a |
@taylorotwell That sounds good to me. Would be good to have that on the Collection class as well for consistency 👍 |
Following on @taylorotwell's comment, this will also make it easier for people while upgrading to just replace |
tests/Http/HttpRequestTest.php
Outdated
|
||
$request = Request::create('/', 'GET', ['developer' => ['name' => 'Taylor', 'age' => 25]]); | ||
$this->assertEquals(['developer' => ['age' => 25]], $request->only('developer.age')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also introduces a change in behavior where only
can't retrieve nested keys. We may want to still have that functionality.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, will look into that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It now supported nested keys as well.
@misenhower - as a side point you can solve your issue another way:
That means an unchecked checkbox will still come through your form request as http://stackoverflow.com/a/476581/1317935 Might mean you can refactor your code to use that, rather than refactor to use the new Ping me on slack if you want to discuss further (dont want to derail this PR) |
@laurencei that is a way to solve it but it feels akward to me. I'd prefer to have the pick() method to be added so I don't have to mess with frontend code. Replacing only() with pick() in the backend will be a lot easier. |
@laurencei Yeah, that's a good option too, I've had to do that in a few situations as well. It works but feels a little hacky (and it adds duplicated fields to query strings if you're submitting via GET). Good to have both options available 😃 |
👍 |
1 similar comment
+1 |
Not only consistency within similarly named methods in the framework, but more coherent behaviour. I think it a bit odd that telling a request you want 👍 |
@@ -149,8 +149,14 @@ public function only($keys) | |||
|
|||
$input = $this->all(); | |||
|
|||
$placeholder = str_random(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool trick 👍
Incoming HTTP requests would have no way to trick this place-holder or even get lucky.
Updated the |
|
+1 |
Switched to using |
One day this is going to happen 😌 #15758 |
Only something to consider from a "social responsibility" point of view. Recently theres been a surge of tweets/blog posts, video tutorials including ones from @adamwathan suggesting the benefits of "Intersect" Now removing it will lead to killing lots of recently released blog posts/resources which could confuse newcomers. |
Happens all the time, just means it's time for new blog content! |
@themsaid are you going to add the |
@vinterskogen Given the recent changes with this topic, it's best to use a macro and wait for Laravel 5.5. You can find an example of a macro with a person's expected version of |
Not sure if I'm at the right place, but will this fix this problem as well:
$request->all()
$request->intersect(array_keys($rules));
|
I was cleaning up some code and had been using filled on a request to ensure that it was in fact filled.
In testing I noticed: Object of class DateTime could not be converted to string because the filled method expects a string. In my case, I don't see any advantage to using filled. so I'm simply going to remove filled and go with:
Nevertheless, I would have expected filled to continue to work; however, I suspect that casting start_date as a date in the Donation model explains the change in behavior (previously on the edit resource view I had renamed the form input name to start_date_only). My question, for those with a greater understanding of how things ought to be, is would it be a helpful tweak or create an issue requesting to modify the filled method to properly handle a DateTime objects or would doing so encourage less than ideal programming? Alternatively, I wonder if an explanation on https://laravel.com/docs/8.x/requests to explain that the filled method will not work with a date would be helpful. |
This will make
Request::only()
work likeCollection::only()
. Currently these two methods are inconsistent with each other. This will bring them into harmony so that they behave the same.Given the following payload:
Output of
request()->only('name', 'category', 'level', 'age')
will be:Prior to this PR,
age
would have been included in the array returned byonly
.