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

[11.x] Add session hasAny method #50897

Merged
merged 3 commits into from
Apr 3, 2024
Merged

Conversation

mahmoudmohamedramadan
Copy link
Contributor

@mahmoudmohamedramadan mahmoudmohamedramadan commented Apr 3, 2024

Laravel ships with the session has method that checks if the passed key is present but, sometimes we need to check if any of the given keys are present. Let's check out the old code and what the code will look like after merging this PR:

Previously

if (session()->has('first_name') || session()->has('last_name')) {
    // do something...
}

Later

if (session()->hasAny(['first_name', 'last_name'])) {
    // do something...
}

Important

I know that there is a collection hasAny method but, In my opinion, it's not the better approach for two reasons:

  1. According to performance, I must check if any passed keys are included in all session keys instead of checking specific keys.
  2. According to empty value checking, I also needed to check if these keys were not null, so I used the collection filter method to count the filtered items.

@mahmoudmohamedramadan
Copy link
Contributor Author

@driesvints It's done 🙋‍♂️

@taylorotwell taylorotwell merged commit 1bf10e6 into laravel:11.x Apr 3, 2024
26 checks passed
@mahmoudmohamedramadan mahmoudmohamedramadan changed the title [11.x] Add Session hasAny method [11.x] Add session hasAny method Apr 3, 2024
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.

2 participants