1. Go to this page and download the library: Download ssntpl/data-fields library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ssntpl / data-fields example snippets
use Ssntpl\DataFields\Support\DataField;
class User extends Model
{
protected $casts = [
'preferences' => DataField::class, // cast mode
];
use \Ssntpl\DataFields\Concerns\HasDataFields; // row mode
}
// Cast mode — work with the column as a typed document
$user->preferences = DataField::section(items: [
['key' => 'dark_mode', 'type' => 'bool', 'value' => true],
['key' => 'language', 'type' => 'text', 'value' => 'en'],
]);
$user->preferences->dark_mode->value; // bool true
$user->preferences->dark_mode->value = false;
$user->save();
// Row mode — store fields polymorphically
$user->fields()->create([
'key' => 'phone', 'type' => 'text', 'value' => '+91-9999900000',
]);
$user->getFieldValue('phone'); // '+91-9999900000'
Schema::table('users', function (Blueprint $table) {
$table->json('preferences')->nullable();
});
Schema::table('users', function (Blueprint $table) {
$table->json('preferences')->nullable();
});
use Ssntpl\DataFields\Support\DataField;
class User extends Model
{
protected $casts = [
'preferences' => DataField::class,
];
protected $fillable = ['preferences', /* ... */];
}
$product->getFieldValue('sku'); // cast value or null
$product->setFieldValue('sku', 'NEW-001'); // upsert by key
$product->setFieldValue('weight', 2.5, 'number'); // type on first set
use Ssntpl\DataFields\Models\DataRow;
class CustomDataRow extends DataRow
{
protected $extraFillable = ['source_system'];
public function getFillable()
{
return array_merge(parent::getFillable(), $this->extraFillable ?? []);
}
public function isFromExternalSystem(): bool
{
return $this->source_system !== null;
}
}
return [
// Row-mode Eloquent model. Subclass DataRow and point at it to add
// custom attributes/behaviour.
'data_row_model' => \Ssntpl\DataFields\Models\DataRow::class,
// Enable created_at / updated_at on the `data_fields` table.
// Off by default — most consumers don't need per-row timestamps.
'data_fields_timestamps' => false,
// When true, the service provider loads the package's migration
// directly — no `vendor:publish` needed.
'auto_load_migrations' => false,
];