Skip to content

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

@n-hor

Description

@n-hor

Laravel Version

12.39

PHP Version

8.4

Database Driver & Version

No response

Description

After the recent changes in
https://github.com/laravel/framework/blame/a925110b0ad6aa4a7bf7a03ee27ab52931b3e8ab/src/Illuminate/Collections/Arr.php#L274

My code started breaking, as every place where I use Arr::first() on a model property cast to AsArrayObject stopped working. I now get the following error:

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

 ErrorException 

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

  at vendor/laravel/framework/src/Illuminate/Collections/Arr.php:274
    270▕ 
    271▕             return value($default);
    272▕         }
    273▕ 
  ➜ 274▕         $key = array_find_key($array, $callback);
    275▕ 
    276▕         return $key !== null ? $array[$key] : value($default);
    277▕     }
    278▕


Steps To Reproduce

Create a model with a property cast to AsArrayObject:

class Product extends Model
{
    protected $casts = [
        'prices' => AsArrayObject::class,
    ];
}

$product = Product::first();

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions