A Laravel package for managing dynamic data fields and data sets with polymorphic relationships. This package allows you to attach custom fields and organized field sets to any Eloquent model.
- Dynamic Data Fields: Attach custom fields to any model
- Data Sets: Organize fields into logical groups
- Polymorphic Relations: Works with any Eloquent model
- Field Validation: Built-in validation support
- Configurable Models: Override default models
- Migration Support: Database migrations included
composer require ssntpl/data-fieldsphp artisan vendor:publish --tag=data-fields-configFor Laravel versions before 5.5, add the service provider to config/app.php:
'providers' => [
// Other providers...
Ssntpl\DataFields\DataFieldsServiceProvider::class,
],php artisan vendor:publish --tag=data-fields-migrations
php artisan migrateThe package publishes a configuration file to config/data-fields.php:
<?php
return [
'data_set_model' => \Ssntpl\DataFields\Models\DataSet::class,
'data_field_model' => \Ssntpl\DataFields\Models\DataField::class,
'data_sets_timestamps' => false,
'data_fields_timestamps' => false,
];use Ssntpl\DataFields\Traits\HasDataFields;
class User extends Model
{
use HasDataFields;
}use Ssntpl\DataFields\Traits\HasDataSets;
class Product extends Model
{
use HasDataSets;
}$user = User::find(1);
// Create a simple field
$user->fields()->create([
'key' => 'phone_number',
'value' => '+1234567890',
'type' => 'text',
'description' => 'User phone number'
]);
// Create field with validation
$user->fields()->create([
'key' => 'age',
'value' => '25',
'type' => 'number',
'validations' => ['required', 'numeric', 'min:18'],
'sort_order' => 1
]);// Get all fields for a model
$fields = $user->fields;
// Get specific field by key
$phoneField = $user->fields()->where('key', 'phone_number')->first();
// Get field value
$phoneNumber = $phoneField->value;$product = Product::find(1);
// Create a data set
$specifications = $product->data_sets()->create([
'name' => 'Product Specifications',
'type' => 'specifications',
'sort_order' => 1
]);
// Add fields to the data set
$specifications->fields()->create([
'key' => 'weight',
'value' => '2.5kg',
'type' => 'text',
'description' => 'Product weight'
]);
$specifications->fields()->create([
'key' => 'dimensions',
'value' => '30x20x10cm',
'type' => 'text',
'description' => 'Product dimensions'
]);// Get all data sets
$dataSets = $product->data_sets;
// Get specific data set by type
$specs = $product->data_sets()->where('type', 'specifications')->first();
// Get fields within a data set
$specFields = $specs->fields;The package supports various field types:
// Text field
$field = $model->fields()->create([
'key' => 'description',
'value' => 'Sample description',
'type' => 'text'
]);
// Number field with validation
$field = $model->fields()->create([
'key' => 'price',
'value' => '99.99',
'type' => 'number',
'validations' => ['required', 'numeric', 'min:0']
]);
// Date field
$field = $model->fields()->create([
'key' => 'expiry_date',
'value' => '2024-12-31',
'type' => 'date',
'validations' => ['required', 'date', 'after:today']
]);Both DataField and DataSet models support duplication:
// Duplicate a data field
$originalField = DataField::find(1);
$duplicatedField = $originalField->duplicate();
// Duplicate a data set (includes all its fields)
$originalSet = DataSet::find(1);
$duplicatedSet = $originalSet->duplicate();You can extend the base models to add custom functionality:
// Custom DataField model
class CustomDataField extends \Ssntpl\DataFields\Models\DataField
{
protected $fillable = ['custom_attribute'];
// Add custom methods
public function getFormattedValue()
{
return strtoupper($this->value);
}
}
// Custom DataSet model
class CustomDataSet extends \Ssntpl\DataFields\Models\DataSet
{
protected $extraFillable = ['custom_field'];
public function getFillable()
{
return array_merge(parent::getFillable(), $this->extraFillable ?? []);
}
}Update your configuration:
// config/data-fields.php
return [
'data_set_model' => App\Models\CustomDataSet::class,
'data_field_model' => App\Models\CustomDataField::class,
];id- Primary keyowner_id- Polymorphic relation IDowner_type- Polymorphic relation typedescription- Field descriptionkey- Field identifiervalue- Field valuetype- Field type (text, number, date, etc.)validations- JSON validation rulessort_order- Display ordermeta_data- Additional metadata
id- Primary keyowner_id- Polymorphic relation IDowner_type- Polymorphic relation typename- Data set nametype- Data set type/categorysort_order- Display ordermeta_data- Additional metadata
fields()- Morphed relationship to data fields
data_sets()- Morphed relationship to data sets- Includes
HasDataFieldstrait
owner()- Polymorphic relationship to owner modelduplicate()- Create a copy of the fielddelete()- Delete field and related data
owner()- Polymorphic relationship to owner modelfields()- Relationship to associated fieldsduplicate()- Create a copy of the set and all fieldsdelete()- Delete set and all associated fields
- PHP 7.4+
- Laravel 8.0+
- Issues: GitHub Issues
- Source: GitHub Repository
Abhishek Sharma
- Email: [email protected]
- Website: https://ssntpl.com