Skip to content

[12.x] Pass flags to custom Json::$encoder #55548

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

Merged
merged 1 commit into from
Apr 24, 2025

Conversation

rodrigopedra
Copy link
Contributor

@rodrigopedra rodrigopedra commented Apr 24, 2025

While reading through closed PR #55537, I realized one could use a custom JSON encoder for Eloquent's built-in casters, by providing a custom callable implementation using:

// assuming MyJsonEncoder is an invokable class,
// this method accepts any callable
Illuminate\Database\Eloquent\Casts\Json::encodeUsing(new MyJsonEncoder());

Then I remembered that, I recently migrated a project from Laravel 8, to Laravel 12, which uses a custom encoder, in a very similar fashion.

But as on Laravel 8, this class wasn't available (it was introduced on Laravel 10, by PR #46552), I ended up duplicating some of the built-in casters to make use of this encoder.

While reviewing the code to simplify it using this approach, I noticed my custom encoder accepts the $flags from the caster, which the built-in encoder does not.

This PR:

  • Updates the Illuminate\Database\Eloquent\Casts\Json::encode() method to pass the $flags argument down to a user provided encoder.

Note: the symmetric Json::decode() static method, already passes the $associative argument down to a user provided decoder.

@taylorotwell taylorotwell merged commit b73ac01 into laravel:12.x Apr 24, 2025
60 checks passed
@rodrigopedra rodrigopedra deleted the json-options branch April 25, 2025 01:29
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