79559496

Date: 2025-04-07 09:35:13
Score: 0.5
Natty:
Report link

Run into the same problem.
My fix was it to add the `array` cast to the model attribute.

/**
 * @return array<string, string>
 */
protected function casts(): array
{
    return [
        'areas' => 'array',
    ];
}

---

Some background.

I create multiple instances of the same model.
Put them into a collection and use mass inserting in the end to increate save speed.

MyModel::insert($models->toArray());

One of the attributes is a mandatory `json` field.
The field is set on the model but `toArray()` remove the content.

---

Beside that I tried `asCollection::class` as described in the documentation:
https://laravel.com/docs/11.x/eloquent-mutators#array-object-and-collection-casting
but it did not work.

Reasons:
  • Blacklisted phrase (1): did not work
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Sergej Tihonov