Skip to content

Comments

[11.x] Add session hasAny method#50897

Merged
taylorotwell merged 3 commits intolaravel:11.xfrom
mahmoudmohamedramadan:11.x
Apr 3, 2024
Merged

[11.x] Add session hasAny method#50897
taylorotwell merged 3 commits intolaravel:11.xfrom
mahmoudmohamedramadan:11.x

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
@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