Download the PHP package trappistes/laravel-custom-fields without Composer
On this page you can find all versions of the php package trappistes/laravel-custom-fields. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download trappistes/laravel-custom-fields
More information about trappistes/laravel-custom-fields
Files in trappistes/laravel-custom-fields
Package laravel-custom-fields
Short Description Flexible EAV package for Laravel to add dynamic custom fields without schema changes / 为 Laravel 设计的高效 EAV 包,无需迁移即可为模型添加动态自定义字段
License MIT
Informations about the package laravel-custom-fields
Laravel Custom Fields
A flexible EAV (Entity-Attribute-Value) package for Laravel Eloquent models, allowing dynamic custom fields without modifying the database schema.
🇺🇸 English
Features
- Dynamic Fields — Add new fields to models without migrations
- Strong Type Support — Supports int, float, bool, json, date, datetime, daterange and more
- Polymorphic Relations — One custom fields table supports any model
- Preloading Optimization — Supports Eloquent preloading to avoid N+1 queries
- Auto Type Conversion — Automatic serialization and deserialization of field values
- Batch Upsert — Efficient bulk field operations via atomic upsert
- Field Validation — Observer-based key format and reserved word validation
- Soft Deletes — Custom fields preserved on soft delete, deleted on force delete
Installation
Publishing Configuration
Configuration
Run Migrations
Quick Start
1. Use Trait in Model
2. Set Custom Fields
3. Get Custom Fields
4. Preload Custom Fields
Supported Field Types
| Type | PHP Type | Storage Format | Example |
|---|---|---|---|
string |
string | raw string | "Hello World" |
bigint |
int | numeric string | "9223372036854775807" |
float |
float | numeric string | "3.14159" |
bool |
bool | "1" or "0" | "1" |
json |
array/object | JSON string | '{"key":"value"}' |
date |
Carbon/string | Y-m-d | "2024-01-15" |
time |
Carbon/string | H:i:s | "14:30:00" |
datetime |
Carbon/string | Y-m-d H:i:s | "2024-01-15 14:30:00" |
daterange |
CarbonPeriod/DatePeriod | JSON | '{"start":"2024-01-01","end":"2024-01-31"}' |
Types are automatically inferred:
Field Naming Rules
- Must start with letter or underscore
- Can only contain letters, numbers and underscores
- Reserved keys:
id,model_type,model_id,field_key,field_type,field_value,created_at,updated_at,deleted_at,exists,incrementing,wasRecentlyCreated
Mass Assignment
Soft Deletes
When the main model uses soft deletes, custom fields are preserved on normal delete and deleted on force delete:
UUID / ULID Support
By default, the id primary key and model_id column use unsignedBigInteger. To support UUID or ULID:
Option 1: Use UUID
-
Modify the migration to use
uuid()for the primary key andmodel_id: - Create a custom model with the
HasUuidstrait:
Option 2: Use ULID
-
Modify the migration to use
ulid()for the primary key andmodel_id: - Create a custom model with the
HasUlidstrait:
Apply the Custom Model
Update your config to use the custom model:
Database Schema
Compatibility
| Laravel | PHP | Status |
|---|---|---|
| 9.x | 8.1+ | Supported |
| 10.x | 8.1+ | Supported |
| 11.x | 8.2+ | Supported |
| 12.x | 8.2+ | Supported |
Testing
License
MIT License