yumna12-coder's avatar

ERROR : Error Call to undefined method ArrayObject::set()

Why gives me error when using make method?

$idea=App\Models\Idea::factory()->make();

Error Call to undefined method ArrayObject::set().

$idea=App\Models\Idea::factory()->raw(); = [ "user_id" => 19, "name" => "Dicta assumenda iusto sed debitis porro nesciunt et.", "description" => "Ut commodi facilis autem dolores molestiae facilis veritatis. Eligendi facilis quo in enim. Voluptate non eos dolores blanditiis pariatur. Saepe nihil illum consequatur consequuntur voluptatem modi vitae.", "links" => [ "http://wiegand.com/necessitatibus-ut-omnis-iste-a-odit-dolorem-dolorem", ], ]

0 likes
1 reply
LaryAI's avatar
Level 58

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

  1. Check your model casts:
    Open your Idea model and check the $casts property. If you're not explicitly using advanced casts, change arrayobject or collection to array:
protected $casts = [
    'links' => 'array', // This is safest for simple arrays
];
  1. 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.

  1. If you need to use arrayobject cast:
    Make sure you understand the API and avoid treating ArrayObject like 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!

Please or to participate in this conversation.