Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Support returning an instance of a caster #32215

Closed
wants to merge 67 commits into from

Conversation

brendt
Copy link
Contributor

@brendt brendt commented Apr 3, 2020

See #32213 (comment)


With the addition of the Castable interface (laravel/docs#5934), it makes sense to allow direct instantiation from within a Castable::castUsing implementation. A common usecase could be something like this:

class EloquentDataTransferObject extends DataTransferObject implements Castable
{
    public static function castUsing()
    {
        return new DataTransferObjectCaster(static::class);
    }
}

Which allows a kind of "generic" cast. All classes extending EloquentDataTransferObject can now directly be used as a cast type, and will be casted to the correct implementation of EloquentDataTransferObject.

Eg.

class MyData extends EloquentDataTransferObject {
    public int $property = 1;
}

class ModelX extends Model
{
    protected $casts = [
        'data' => MyData::class,
    ];
}

@brendt
Copy link
Contributor Author

brendt commented Apr 3, 2020

@taylorotwell I suppose this means it'll be added in 8.x and not 7.x?

@mfn
Copy link
Contributor

mfn commented Apr 3, 2020

Would be great to have this in 7.x … it's backwards compatible, no?

@GrahamCampbell GrahamCampbell changed the title Support returning an instance of a caster [8.x] Support returning an instance of a caster Apr 3, 2020
@driesvints
Copy link
Member

@brendt correct.

@mfn Taylor asked to send this to master instead.

@taylorotwell taylorotwell changed the base branch from master to 7.x April 3, 2020 15:49
@taylorotwell
Copy link
Member

OK then send back to 7.x ... haha... I'll just merge it forward. Sorry for the inconvenience of making you send again. 😬

@brendt
Copy link
Contributor Author

brendt commented Apr 4, 2020

No problem, here it is: #32225

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants