Getting started
Laravel Attribute Mapper maps validated Form Request fields to an Eloquent model's fillable attributes. Use the mapped attributes to create a record or update only the fields submitted.
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.
sh
composer require mattmy/laravel-attribute-mapperConfiguration
The default Exact style matches input keys to identical model attribute names. Set a naming style on a mapping with inputKeys(), or publish the configuration to set an application default.
Quick start
Create this Form Request in app/Http/Requests/MappingExampleRequest.php.
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.
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"}. Use the same mapping in your create and update controllers to build the attribute array.