PHP code example of trappistes / laravel-custom-fields

1. Go to this page and download the library: Download trappistes/laravel-custom-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/ */

    

trappistes / laravel-custom-fields example snippets


// config/custom-fields.php
return [
    /*
    |--------------------------------------------------------------------------
    | Custom Field Model
    |--------------------------------------------------------------------------
    |
    | You can override the default CustomField model to customize the primary key
    | type (e.g., UUID, string) or add additional functionality.
    |
    */
    'model' => \Trappistes\CustomFields\Models\CustomField::class,

    /*
    |--------------------------------------------------------------------------
    | Custom Table Name
    |--------------------------------------------------------------------------
    |
    | Customize the table name for custom fields.
    |
    */
    'table_name' => 'custom_fields',

    /*
    |--------------------------------------------------------------------------
    | JSON Maximum Depth
    |--------------------------------------------------------------------------
    |
    | Maximum nesting depth for JSON encoding/decoding to prevent DoS attacks.
    |
    */
    'json_max_depth' => 64,
];

use Trappistes\CustomFields\Traits\HasCustomFields;

class User extends Model
{
    use HasCustomFields;
}

$user = User::find(1);

// Set single field
$user->setCustomField('nickname', 'John');
$user->setCustomField('age', 25);
$user->setCustomField('is_vip', true);
$user->setCustomField('preferences', ['theme' => 'dark', 'lang' => 'en']);

// Batch set (uses upsert for performance)
$user->setCustomFields([
    'nickname' => 'John',
    'age' => 25,
    'is_vip' => true,
]);

$user = User::find(1);

// Get single field
$nickname = $user->getCustomField('nickname');
$age = $user->getCustomField('age', 18); // with default value

// Using dynamic property
$nickname = $user->nickname;

// Check if field exists
$hasNickname = $user->hasCustomField('nickname');

// Get all custom fields
$allFields = $user->getAllCustomFields();

// Recommended: use preloading to avoid N+1 queries
$users = User::with('customFields')->get();

foreach ($users as $user) {
    echo $user->getCustomField('nickname'); // no extra query
}

$user->setCustomField('metadata', ['key' => 'value']); // auto inferred as json
$user->setCustomField('count', 42); // auto inferred as bigint

$user = User::find(1);

// fill() method also supports custom fields
$user->fill([
    'name' => 'John',
    'nickname' => 'nick',  // custom field
    'age' => 25,           // custom field
])->save();

// update() method separates regular and custom fields
$user->update([
    'name' => 'Updated Name',
    'department' => 'Engineering', // custom field
]);

$user = User::find(1);

// Soft delete - custom fields preserved
$user->delete();

// Force delete - custom fields deleted too
$user->forceDelete();

// Restore - custom fields still available
$user->restore();

// In your migration file
$table->uuid('id')->primary();
$table->uuid('model_id')->nullable();

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Trappistes\CustomFields\Models\CustomField as BaseCustomField;

class CustomField extends BaseCustomField
{
    use HasUuids;
}

// In your migration file
$table->ulid('id')->primary();
$table->ulid('model_id')->nullable();

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Trappistes\CustomFields\Models\CustomField as BaseCustomField;

class CustomField extends BaseCustomField
{
    use HasUlids;
}

// config/custom-fields.php
'model' => App\Models\CustomField::class,
bash
php artisan vendor:publish --provider="Trappistes\CustomFields\Providers\CustomFieldsServiceProvider"
bash
php artisan migrate