Skip to content

Conversation

@prince-noman
Copy link
Contributor

This PR fixes a regression where Arr::first() fails when the input is an ArrayObject or an Eloquent cast using AsArrayObject.

Summary

In Laravel 12.39, Arr::first() was updated to use PHP 8.4's new array_find_key() function.
However, array_find_key() only accepts native arrays, not ArrayObject or other array-like values.

This causes a type error when calling Arr::first() on model attributes cast using AsArrayObject, e.g.:

$product = Product::first();

Arr::first($product->prices, fn ($price) => $price === 0);

Error:

array_find_key(): Argument #1 ($array) must be of type array, Illuminate\Database\Eloquent\Casts\ArrayObject given

Fix

This PR normalizes the input using Arr::from() before passing it to array_find_key(), restoring support for:

  • ArrayObject
  • Illuminate\Database\Eloquent\Casts\ArrayObject (Eloquent AsArrayObject)
  • other Traversable / array-like values that worked before the regression

The behavior is fully backward-compatible.

Related Issue

Fixes #57959

@taylorotwell taylorotwell merged commit 263ea42 into laravel:12.x Nov 29, 2025
74 checks passed
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.

Arr::first() helper not working with model property cast to AsArrayObject

3 participants