The error "Call to undefined method ArrayObject::set()" often happens when you have a model attribute (like a column in your database) that is being cast to ArrayObject, but you're treating it like a normal array or object that has a set method—which ArrayObject doesn’t. In Laravel, this mistake often happens with casts in your model, especially if you use the asArrayObject or asCollection cast:
For example, you might have this in your model:
protected $casts = [
'links' => 'array', // typical case
// OR accidentally:
'links' => 'arrayobject' // notice this!
];
or (since Laravel 9):
protected $casts = [
'links' => 'collection', // or 'arrayobject'
];
Why does the error happen with factory()->make()?
Factories use the model's normal attribute handling and casting, and if you have an attribute being cast to arrayobject or collection, Laravel wraps the array data with ArrayObject or Collection. When you later interact with $idea->links, it tries to wrap or "set" the ArrayObject, but your usage or the framework’s internal mechanism is expecting a normal array.
Solution
- Check your model casts:
Open yourIdeamodel and check the$castsproperty. If you're not explicitly using advanced casts, changearrayobjectorcollectiontoarray:
protected $casts = [
'links' => 'array', // This is safest for simple arrays
];
- Use
make()for models,raw()for plain arrays:
factory()->make()returns an Eloquent model instance (not persisted).factory()->raw()returns a plain attribute array.
Stick with ->make() for models unless you want a plain array.
- If you need to use
arrayobjectcast:
Make sure you understand the API and avoid treatingArrayObjectlike a plain array. For most use cases,'array'is best.
Example Fix
Check your Idea model (usually at app/Models/Idea.php):
class Idea extends Model
{
protected $casts = [
'links' => 'array', // Use 'array', not 'arrayobject' or 'collection' unless needed
];
// ... rest of the model
}
Summary:
Change the problematic cast in the model to 'array', then your factory code will work as expected:
$idea = App\Models\Idea::factory()->make();
Let me know if you need further explanation or run into other issues!