json-mapper/eloquent-middleware
Laravel middleware package for integrating JsonMapper with Eloquent, helping map JSON payloads to models and serialize models back to JSON. Reduces boilerplate for API request and response handling.
jsonmapper/eloquent-middleware plugs into JsonMapper’s middleware pipeline so JSON payloads can hydrate Eloquent models instead of plain DTOs. Install it with Composer, then check the README and src/ for the exact middleware namespace and registration API. In Laravel, package discovery should register any service provider automatically; otherwise add it to config/app.php.
Typical first setup:
use JsonMapper\JsonMapperFactory;
use JsonMapper\Middleware\EloquentMiddleware;
use App\Models\User;
$mapper = (new JsonMapperFactory())->bestFit();
$mapper->addMiddleware(new EloquentMiddleware());
$user = $mapper->map(json_decode($payload), User::class);
$user->save();
Start with a simple import/webhook use case: take an API JSON object, map it to a model, inspect $user->getAttributes(), then persist. This confirms attribute mapping, casts, and mass-assignment behavior before you build larger ingestion flows.
snake_case or camelCase but your model attributes differ. Register case conversion before the Eloquent middleware.save(). For updates, map the payload, then apply attributes to an existing model or use updateOrCreate() with the mapped attribute array.setRelation() or explicit related-model creation for hasMany/belongsTo data.upsert() or updateOrCreate() to avoid duplicate rows and excessive queries.getAttributes(), verify casts with getCasts()/attribute access, and assert database state after save().exists is false, timestamps are not populated until save(), and model events fire at persistence time, not mapping time.forceFill() or raw attribute assignment, $fillable/$guarded may be bypassed. Validate and whitelist input at the boundary.$guarded = ['*'] is the Eloquent default and can block fill(). Know whether the middleware force-fills or respects guarding; inspect $model->isDirty() and getAttributes() when debugging.getAttributes() returns raw attributes, while toArray() may expose hidden/appended attributes and cast values. Use raw attributes for debugging and comparison.Carbon, enums, or custom value objects where needed.php artisan config:clear/config:cache.getAttributes(), getCasts(), getDirty(), and the mapped model class before saving. Enable JsonMapper’s debug middleware to see how JSON keys map to properties.How can I help you explore Laravel packages today?