laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

Eloquent, collection cast #1324

Open atrauzzi opened 5 years ago

atrauzzi commented 5 years ago

Love being able to have DB JSON types cast to arrays using the 'json' $casts property.

One thing I noticed was that because arrays are always copied (rather than referenced), you end up with verbose code when you want to mutate the value:

$something->json_backed_attribute = array_merge($something->json_backed_attribute, [
    MyEnum::MY_CONST => [
        'some-thing' => "doesn't matter",
    ],
]);

This got me thinking that it would be great if there was something like a json_collection cast that returned an instance wrapped by collect().

Benefits:


Love it? Hate it? It was just a thought I had this morning... 😄

staudenmeir commented 5 years ago

There already is a collection cast, but this doesn't solve the mutation issue. See #1275.

andrey-helldar commented 4 years ago

@atrauzzi, if I understand your problem correctly, the changes proposed in laravel/framework#30958 will make it easy to solve.