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

[7.x] Support returning an instance of a caster #32213

Closed
wants to merge 1 commit into from

Conversation

brendt
Copy link
Contributor

@brendt brendt commented Apr 3, 2020

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

If this gets merged, I'll also submit a PR targeting Laravel 8, which allows passing the arguments directly to public static function castUsing(array $arguments).

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

Send this to master please. I already made changes to this on master.

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.

2 participants