Skip to content
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.3] Use intersect instead of only in the request helper #14611

Closed
wants to merge 1 commit into from

Conversation

JosephSilber
Copy link
Member

@JosephSilber JosephSilber commented Aug 3, 2016

request()->only() is a little weird, in that it returns null for missing values:

// $_POST === ['a' => 1, 'c' => 3];

request()->only('a', 'b'); // ['a' => 1, 'b' => null]

This is quite weird, as array_only skips missing values completely. I guess it's too late to change Request@only now, so a while ago the intersect method was added, which mimics array_only:

// $_POST === ['a' => 1, 'c' => 3];

request()->intersect('a', 'b'); // ['a' => 1]

This is the behavior you'd want most of the time. Imagine you're sticking stuff into the database:

$post->update(request(['title', 'body', 'date']));

If the user hasn't provided one of those properties, you definitely do not want to set those to null!

@JosephSilber JosephSilber changed the title Use intersect instead of only in the request helper [5.3] Use intersect instead of only in the request helper Aug 3, 2016
@GrahamCampbell
Copy link
Member

I prefer only. It guarantees all the keys will exist.

@taylorotwell
Copy link
Member

Eh, this seems like personal preference to me. If we change this, no doubt someone will come along who expected the other behavior and who is to say who is correct in that situation?

@JosephSilber JosephSilber deleted the request-intersect branch December 14, 2016 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants