PHPackages                             spykapp/laravel-custom-fields - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. spykapp/laravel-custom-fields

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

spykapp/laravel-custom-fields
=============================

Laravel Package for Managing Custom Fields for Each Model.

v1.0.1(1y ago)223MITPHPPHP ^8.2

Since Oct 25Pushed 1y agoCompare

[ Source](https://github.com/SpykApp/laravel-custom-fields)[ Packagist](https://packagist.org/packages/spykapp/laravel-custom-fields)[ Docs](https://github.com/spykapp/laravel-custom-fields)[ GitHub Sponsors](https://github.com/SpykApp)[ RSS](/packages/spykapp-laravel-custom-fields/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (12)Versions (3)Used By (0)

[![Cover Image](https://camo.githubusercontent.com/131065bb1adfccbdc615853662f9bbb263f8ea1aa7b7929ea5558e3063c98b16/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f6c61726176656c2d637573746f6d2d6669656c64732e706e673f7468656d653d6461726b267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d7370796b6170702532466c61726176656c2d637573746f6d2d6669656c6473267061747465726e3d617574756d6e267374796c653d7374796c655f31266465736372697074696f6e3d4c61726176656c2b5061636b6167652b666f722b4d616e6167696e672b437573746f6d2b4669656c64732b666f722b456163682b4d6f64656c266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667)](https://camo.githubusercontent.com/131065bb1adfccbdc615853662f9bbb263f8ea1aa7b7929ea5558e3063c98b16/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f6c61726176656c2d637573746f6d2d6669656c64732e706e673f7468656d653d6461726b267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d7370796b6170702532466c61726176656c2d637573746f6d2d6669656c6473267061747465726e3d617574756d6e267374796c653d7374796c655f31266465736372697074696f6e3d4c61726176656c2b5061636b6167652b666f722b4d616e6167696e672b437573746f6d2b4669656c64732b666f722b456163682b4d6f64656c266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667)

Laravel Package for Managing Custom Fields for Each Model
=========================================================

[](#laravel-package-for-managing-custom-fields-for-each-model)

[![Latest Version on Packagist](https://camo.githubusercontent.com/18c20a368853c29ef2a849f5dc6d9286a686bcef74769fd6eeaa4a12f7d778b7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370796b6170702f6c61726176656c2d637573746f6d2d6669656c64732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spykapp/laravel-custom-fields)[![GitHub Tests Action Status](https://camo.githubusercontent.com/adf58df883a33db2bb1063afc1108c76302c934ffef6216d1e4967114f5656c8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7370796b6170702f6c61726176656c2d637573746f6d2d6669656c64732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/spykapp/laravel-custom-fields/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/2368136660beb609ef01dea3c27f458afc488760e9dd353b3c726dec6e4469fe/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7370796b6170702f6c61726176656c2d637573746f6d2d6669656c64732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/spykapp/laravel-custom-fields/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/ed10da9de37b247e2302bdf43eb29dab0bfe5abcb9366aa6f05d752d4042f59d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370796b6170702f6c61726176656c2d637573746f6d2d6669656c64732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spykapp/laravel-custom-fields)

This Laravel package allows you to dynamically add custom fields to any model in your application. It supports storing custom fields as polymorphic relationships and offers dynamic loading of custom field responses directly into the model’s attributes, including support for default values.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spykapp/laravel-custom-fields
```

You can publish and run the migrations with:

```
php artisan vendor:publish --tag="custom-fields-migrations"
php artisan migrate
```

You can publish the config file with:

```
php artisan vendor:publish --tag="custom-fields-config"
```

This is the contents of the published config file:

```
return [
    'tables' => [
        'custom_fields' => 'custom_fields',
        'custom_field_responses' => 'custom_field_responses',
    ],
    'field_prefix' => 'cf_', // Custom prefix for custom field attributes
];
```

Usage
-----

[](#usage)

### Configuration

[](#configuration)

You can customize the table names and field prefixes by publishing the configuration file and making changes to it.

### Overriding Models

[](#overriding-models)

You may want to extend or override the `CustomField` or `CustomFieldResponse` models provided by this package. To do this, you can publish the configuration file and update it to use your own custom models.

### Trait for Managing Custom Fields

[](#trait-for-managing-custom-fields)

To use this package, add the `HasCustomFields` trait to any model where you want to allow custom fields. This trait allows you to easily add, update, and retrieve custom fields and their responses.

```
use SpykApp\LaravelCustomFields\Traits\HasCustomFields;

class Post extends Model
{
    use HasCustomFields;

    // Your model code here...
}
```

Now you can add custom fields for this model, save responses, and retrieve them as attributes with a configurable prefix.

### Add Custom Fields

[](#add-custom-fields)

You can create a custom field for the model like this:

```
use App\Models\Post;
use SpykApp\LaravelCustomFields\Models\CustomField;

CustomField::create([
    'name' => 'subtitle',
    'label' => 'Subtitle',
    'field_type' => 'text',
    'default_value' => 'Default Subtitle',
    'model_type' => Post::class,
]);
```

Below is a list of fields used in the `CustomField` model and their purposes:

- **`name`**: The unique identifier for the custom field. This is used as the reference name for accessing the custom field's value.
- **`label`**: The human-readable label for the field, which will be displayed in the forms.
- **`placeholder`**: Placeholder text that will appear inside input fields (useful for text inputs).
- **`rules`**: Laravel validation rules for the field. You can define validation rules like `'required'`, `'email'`, etc., to validate user input. This field should be stored in JSON format.
- **`classes`**: Additional CSS classes to apply to the field's HTML element. This allows for custom styling of each field.
- **`field_type`**: Specifies the type of the field (e.g., `text`, `textarea`, `select`, `radio`, `checkbox`). This determines what kind of input element will be generated in the form.
- **`options`**: This is used specifically for fields like `select`, `radio`, or `checkbox` to store the available options. It is stored as JSON in the database. Example: `{"options": ["Option 1", "Option 2", "Option 3"]}`.
- **`default_value`**: The default value that will be used if no response is provided by the user. This is useful for setting a fallback value.
- **`description`**: A longer description that explains the purpose or use of the field. This can be shown as part of the form to provide context to the user.
- **`hint`**: A hint or tooltip that can be displayed next to the field to help users understand what the field is for.
- **`sort`**: This is an integer used to order the fields. Fields with lower `sort` values will be displayed before fields with higher `sort` values.
- **`category`**: This allows you to group custom fields into categories. For instance, you might have custom fields categorized as `personal_info` and others as `job_details`. When rendering the form, fields can be grouped by their category.
- **`model_type`**: This is the model class name that the custom field applies to. For example, `App\Models\Post` if the custom field is attached to the `Post` model.
- **`entity_id`**: This is used for multi-tenant applications. Each tenant can have their own set of custom fields by specifying a unique `entity_id`. This ensures that custom fields are isolated per tenant.

### Save Custom Field Responses

[](#save-custom-field-responses)

You can save responses for custom fields like this:

The `saveCustomFieldResponses` method is used to either **insert** or **update** responses for custom fields tied to a specific model record. This ensures that if a response already exists for a given field on a record, it will be updated, and if not, a new response will be inserted.

```
$post = Post::find(1);
$post->saveCustomFieldResponses([
  [
    'custom_field_id' => 1,
    'value' => 'This is the subtitle',
  ],
  [
    'custom_field_id' => 2,
    // No value provided, so it will use default_value
  ]
], validate: false);
```

You may pass `validate : false` as a second parameter to bypass the validations which is ON by default.

### Loading Custom Fields with Responses

[](#loading-custom-fields-with-responses)

You can simply load the custom field responses by calling the model with relationships like this:

```
Post::with(['customFieldResponses.customField'])->get();
```

This will retrieve all `Post` records along with their custom field responses and the related custom fields.

You can also fetch a single post with its custom fields and responses like this:

```
Post::with(['customFieldResponses.customField'])->find(1);
```

#### Using Helper Function

[](#using-helper-function)

For a single Post, you can use the helper function `getCustomFieldsWithResponses()` to load the custom fields and their responses:

```
$post = Post::find(1);
$post->getCustomFieldsWithResponses();
```

This will load the custom fields and their associated responses for the given post instance, making them easily accessible.

#### Using the `LoadCustomFields` Trait

[](#using-the-loadcustomfields-trait)

Alternatively, you can use our second trait, `LoadCustomFields`, which automatically attaches the custom fields and their responses as attributes to each model instance.

Custom field keys will be prefixed by a string that you can configure in the configuration file. By default, the prefix is cf\_, but you can change it in the config.

```
use SpykApp\LaravelCustomFields\Traits\HasCustomFields;
use SpykApp\LaravelCustomFields\Traits\LoadCustomFields;

class Post extends Model
{
    use HasCustomFields, LoadCustomFields;

    // Your model code here...
}
```

Using the LoadCustomFields trait, you can access custom fields directly as attributes:

```
$post = Post::find(1);
echo $post->cf_subtitle; // Outputs the value of the 'subtitle' custom field, or null if not set
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Credits
-------

[](#credits)

- [Sanchit Patil](https://github.com/sanchitspatil)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~13 days

Total

2

Last Release

547d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/619d85ca59369ebee5c640beac15488b61f6d80e4cbb170d82ea422e665341bd?d=identicon)[sanchitpatil](/maintainers/sanchitpatil)

---

Top Contributors

[![sanchitspatil](https://avatars.githubusercontent.com/u/106558065?v=4)](https://github.com/sanchitspatil "sanchitspatil (9 commits)")

---

Tags

laravellaravel-custom-fieldsSpykApp

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/spykapp-laravel-custom-fields/health.svg)

```
[![Health](https://phpackages.com/badges/spykapp-laravel-custom-fields/health.svg)](https://phpackages.com/packages/spykapp-laravel-custom-fields)
```

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.7k28.9M626](/packages/spatie-laravel-data)[hirethunk/verbs

An event sourcing package that feels nice.

513162.9k6](/packages/hirethunk-verbs)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
