I am using Laravel 10.
I am utilizing casting for a JSON column in the following manner:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
protected $casts = [
'meta' => 'collection', // here
];
}
When attempting to update a value within a collection directly, for instance:
$model->meta->put('test', 100);
$model->save();
Nothing occurs.
When I assign the variable as it is, it functions correctly.
$model->meta = ['test' => 100];
$model->save();
However what if I only need to update/add a single element?
I have discovered the following workaround, but is this the intended behavior?
$meta = $model->meta;
$meta->put('test', 100);
$model->meta = $meta;
$model->save();
It appears that only direct assignment works in such a case, and it seems that the cast collection does not support any of its write functionality.