-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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.4] Update Request::intersect to use array_intersect_key #15758
Conversation
We already have a different method for that if you want null. |
Which method would that be? |
And what about false? If I post false then I would expect intersect to return that value |
I think we need that function, but @RDelorier what about create a new function or add an option parameter to old function since @taylorotwell can't change the old one right now for the compatibility. |
I've been shut down twice with this so I'm gonna let it be, I'll just continue to use my macros for now. |
@RDelorier do you mind to share your macros here? I think it's helpful for others whoever face this same situation. |
@GrahamCampbell can you answer the question about what method they are supposed to use. |
@shwhl here is the macro I'm currently using for this // in my AppServiceProvider
\Request::macro('intersectKeys', function ($keys) {
return array_intersect_key($this->all(), array_flip($keys));
}); And I use it instead of Route::post('/test', function (Illuminate\Http\Request $request) {
dd([
'intersect' => $request->intersect(['a', 'b', 'c', 'd']),
'intersectKeys' => $request->intersectKeys(['a', 'b', 'c', 'd'])
]);
}); Then when I post this {
"a": null,
"b": "",
"c": false,
"d": "foo",
"e": "bar"
} I get this result
|
@RDelorier thank you! that's really helpful, I forget we can add macros, I was thinking to create a package for this before. |
What is the difference between this and $request->only()? |
After a quick unit test it seems that ->only does what you want. |
@taylorotwell if |
Like @shwhl said, request |
@taylorotwell here is another example to show the difference in $request->only Request::macro('intersectKeys', function ($keys) {
return array_intersect_key($this->all(), array_flip($keys));
});
$request = Request::create('/', 'GET', [
'a' => 'foo',
'b' => null,
'c' => false,
]);
dd([
'only' => $request->only(['a', 'b', 'c', 'd']),
'intersect' => $request->intersect(['a', 'b', 'c', 'd']),
'intersectKeys' => $request->intersectKeys(['a', 'b', 'c', 'd'])
]); result:
|
I've also stumbled into this issue today. My particular use case is to implement a patch request. Take the following example, to partially update a user's details:
In this example, the email will be updated - but not the |
Related: #17842 (comment) |
This will prevent filtering null and falsy values when using Request intersect.
given
Before
After
moved from #15417