8000 Eloquent, collection cast · Issue #1324 · laravel/ideas · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jul 16, 2021. It is now read-only.
This repository was archived by the owner on Jul 16, 2021. It is now read-only.
Eloquent, collection cast #1324
@atrauzzi

Description

@atrauzzi

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:

  • Feature is still opt-in
  • The value is now passed by reference rather than copied
  • You can now do this:
        $something->json_backed_attribute->merge([]);

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0