novius/laravel-json-casted
Cast JSON columns in Laravel models with per-key casting. Define sub-key casts via a method or dedicated cast class; values are exposed as a Fluent object with proper types (e.g., Carbon dates). Supports PHP 8.2+ and Laravel 10+.
$model->json->key) may require updates if Laravel’s Eloquent evolves (e.g., new casting mechanisms).casting, casted), which could complicate debugging or logging.NullPointerException when accessing $model->extras->date if extras is null). Teams will need to add guards or use Laravel’s whenFilled() for robustness.Log::error) to track issues like invalid date formats.$model->extras->date) triggers Laravel’s casting pipeline, which may introduce latency for high-frequency reads. Benchmark against native accessors/mutators.jsonb or MySQL’s JSON functions to offload casting to the database.fresh() or replicate() for critical sections.->> in PostgreSQL).serialize() or external storage (e.g., Redis) for large payloads.protected static function booted()
{
static::saving(function ($model) {
if ($model->extras && !json_validate($model->extras)) {
throw new \InvalidArgumentException('Invalid JSON in extras field');
}
});
}
"true" to a boolean may fail if the format is unexpected. Use custom cast classes to handle edge cases.$model->extras->nonexistent throws ErrorException. Mitigate with:
$model->extras->get('date', default: null);
json_decode($json, true) with JSON_THROW_ON_ERROR to fail fast.$model->json->key vs. $model->json['key']).laravel-ide-helper to enable autocompletion for casted fields.$model->shouldReceive('getExtrasCasts')->andReturn(['date' => 'date:Y-m-d']);
$model->json['key'] will need refactoring. Use feature flags or gradual migration to minimize disruption.ErrorException or InvalidArgumentException related to JSON parsing.How can I help you explore Laravel packages today?