Skip to content

[12.x] Introducing Arr::hasAll #55815

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

Merged
merged 3 commits into from
May 22, 2025

Conversation

devajmeireles
Copy link
Contributor

@devajmeireles devajmeireles commented May 22, 2025

Continuation of #55770, #26543


Previously, I made a new attempt to introduce hasAll due to frequent needs, but for the wrong target, Collections. Following PR #55770 I am now targeting the correct place, the Arr helper.

Example

use Illuminate\Support\Arr;

$array = ['name' => 'Taylor', 'language' => 'php'];

Arr::hasAll($array, ['name']); // ✅
Arr::hasAll($array, ['name', 'language']); // ✅
Arr::hasAll($array, ['name', 'ide']); // ❌, $array does not have all these keys

Although there are manual ways - PHP only, to achieve the same result without relying on the Arr helper, this brings benefits to framework users in favor of having a helper ready to use, without relying on framework macros or PHP codes written when necessary.

Behind the scenes, we ensure that we use the native has, so that dot notation is fully supported in it.

Co-authored-by: Muhammad Rizky Rizaldi  <[email protected]>
@taylorotwell taylorotwell merged commit 87fa7e2 into laravel:12.x May 22, 2025
59 checks passed
@martinbean
Copy link
Contributor

martinbean commented Jun 11, 2025

@devajmeireles I might be missing something, but what does this do differently to just passing an array of keys as the second argument to the Arr::has method? As that already has the same functionality from what I can tell:

$data = [
    'foo' => 'foo',
];

// Will return false as $data does not have 'bar' key...
Illuminate\Support\Arr::has($data, ['foo', 'bar']);

I ask as I used Arr::has for this very purpose quite recently (checking a Stripe webhook event had multiple metadata keys I was expecting the payload to include).

@AhmedAlaa4611
Copy link
Contributor

What is the difference between Arr::has and Arr::hasAll methods?

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.

5 participants