Search by

mattmy / laravel-attribute-mapper

mattmy

Map validated Laravel Form Request fields to fillable Eloquent attributes using naming rules and aliases.

Package info

github.com/mattmy/laravel-attribute-mapper

Homepage

pkg:composer/mattmy/laravel-attribute-mapper

Statistics

Installs: 8

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.1.2 2026-11-26 08:55 UTC

This package is auto-updated.

Last update: 2026-11-26 13:29:30 UTC


README

繁體中文

Laravel Attribute Mapper turns a Form Request's validated data into the attribute array you need to create or update an Eloquent model.

  • Reuse your model's $fillable list to limit which attributes request data can populate.
  • Match camelCase input to snake_case attributes with inputKeys(InputKeyStyle::Camel). Use an alias to choose the input key for a specific attribute.
  • Keep or exclude specific attributes and add values from your app, such as the editor's ID or a default status.

Requirements

Laravel Composer compatibility PHP versions in the CI matrix
12.x PHP 8.2 or later in the 8.x series 8.2, 8.3, 8.4, 8.5
13.x PHP 8.3 or later in the 8.x series 8.3, 8.4, 8.5

Installation

Run this from your Laravel project root.

composer require mattmy/laravel-attribute-mapper

Quick start

Create this Form Request in app/Http/Requests/MappingExampleRequest.php.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class MappingExampleRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'startTime' => ['sometimes', 'date'],
            'endTime' => ['sometimes', 'nullable', 'date'],
        ];
    }
}

Add this preview route to routes/web.php.

use App\Http\Requests\MappingExampleRequest;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Route;
use Mattmy\FillMap\AttributeMapper;
use Mattmy\FillMap\InputKeyStyle;

Route::get('/mapping-example', static function (MappingExampleRequest $request): array {
    $model = new class() extends Model
    {
        protected $fillable = ['start_time', 'end_time'];
    };

    return AttributeMapper::from($request)
        ->forModel($model)
        ->inputKeys(InputKeyStyle::Camel)
        ->toArray();
});

Visit /mapping-example?startTime=2026-12-01&endTime=2026-12-02 to get {"start_time":"2026-12-01","end_time":"2026-12-02"}.

When your app already has the data in an array, you can pass that array to AttributeMapper::from().

Documentation

Getting started · API reference

Changelog · MIT License