You're running into this error because of how you're defining the $casts property in your model.
Problem:
protected $casts;
This line declares the property, but does not initialize it as an array. By default, it's null, which causes array_merge() to fail when Laravel tries to merge it with the result of your casts() method.
Solution:
Initialize $casts as an empty array:
protected $casts = [];
Full Example:
class Secrets extends Model
{
use HasFactory;
protected $casts = [];
protected function casts(): array
{
return [
'secret' => 'encrypted',
];
}
}
Summary:
Always initialize the $casts property as an array, even if you plan to define your casts using the casts() method. This will prevent the array_merge() error when your table is empty or when no casts are defined in the property itself.