PHPackages                             informatica\_mobius/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. [Database &amp; ORM](/categories/database)
4. /
5. informatica\_mobius/laravel-custom-fields

ActiveLibrary[Database &amp; ORM](/categories/database)

informatica\_mobius/laravel-custom-fields
=========================================

Laravel Custom Fields is a package that allows you to add custom fields to any Laravel model and store responses to those fields on any Laravel model.

1.0.3(1y ago)031MITPHPPHP ^8.1

Since May 17Pushed 1y ago1 watchersCompare

[ Source](https://github.com/InformaticaMobius/laravel11-custom-fields)[ Packagist](https://packagist.org/packages/informatica_mobius/laravel-custom-fields)[ Docs](https://github.com/InformaticaMobius/laravel11-custom-fields)[ RSS](/packages/informatica-mobius-laravel-custom-fields/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (4)Used By (0)

Laravel11 Custom Fields
=======================

[](#laravel11-custom-fields)

Laravel Custom Fields is a package that allows you to add custom fields to any Laravel11 model and associate responses to those fields with other models.

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

[](#installation)

To get started, add the `informatica_mobius/laravel-custom-fields` package to your `composer.json` file and update your dependencies:

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

Publish the migration:

```
php artisan vendor:publish --provider="InformaticaMobius\LaravelCustomFields\LaravelCustomFieldsServiceProvider" --tag="migrations"
```

Run the migration:

```
php artisan migrate
```

*You can customize the table names using configuration options. More on that later.*

An example - Survey App
-----------------------

[](#an-example---survey-app)

For the purposes of the documentation, lets use the example of a Survey building app. Administrators might use a backend to create `Surveys` full of questions and end users might then fill out those surveys, generating `SurveyResponses`.

Preparing your models
---------------------

[](#preparing-your-models)

### Adding custom fields

[](#adding-custom-fields)

To add basic custom field support, simply add the `HasCustomFields` trait at the top of your model:

```
use Illuminate\Database\Eloquent\Model;
use InformaticaMobius\LaravelCustomFields\Traits\HasCustomFields;

class Survey extends Model
{
    use HasCustomFields;

    // ...
}
```

### Adding custom field responses

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

Next, we add support to store custom field responses. We'll simply pull in the `HasCustomFieldResponses` trait at the top of our `SurveyResponse` model.

```
use Illuminate\Database\Eloquent\Model;
use InformaticaMobius\LaravelCustomFields\Traits\HasCustomFieldResponses;

class SurveyResponse extends Model
{
    use HasCustomFieldResponses;

    // ...
}
```

Basic usage
-----------

[](#basic-usage)

### Creating fields

[](#creating-fields)

You can add a field to a model like this:

```
$survey->customFields()->create([
    'title' => 'What is your name?',
    'type' => 'text'
]);
```

Each field can contain the following: (More on these later)

`title` : The title / question of your custom field. `description` : The description of your field. Useful for providing more context to user filling out fields. `type` : The type of field you're creating. Available types are outlined in the next section. `required` : A boolean representing whether a field is required or not. `answers` : An array of acceptable values for fields that have user-selection.

### Creating field responses

[](#creating-field-responses)

To store a response on a field, you can do this:

```
$field->responses()->create([
    'value' => 'John Doe'
]);
```

### Retrieving fields

[](#retrieving-fields)

To retrieve custom fields, use the `customFields` relation:

```
$survey->customFields()->get();
```

### Retrieving field responses

[](#retrieving-field-responses)

To retrieve the responses on a field, use the `responses()` relation:

```
$field->responses()->get();
```

Custom field types
------------------

[](#custom-field-types)

Custom fields may be any of 5 types:

- `text` : Free entry fields which are stored as strings. Use these for simple inputs as they have a max-length of 255 characters.
- `textarea` : Free entry fields which are stored as text columns. Use these for longer bits of text that may not fit within the `text` field.
- `radio` : These are multi-select fields, which require you to pass an `answers` property.\*
- `select` : These are multi-select fields, which require you to pass an `answers` property.\*
- `checkbox` : Boolean fields.

In general, these field types correspond to their respective HTML elements. In the future we may provide front-end scaffolding for these fields, but for now, that's up to you.

\*The `radio` and `select` field types require you to fill the `answers` property on the field. This is a simple array of strings, which are valid responses for the field. For example:

```
$survey->customFields()->create([
   'title' => 'What is your favorite color?',
   'type' => 'select',
   'answers' => ['Red', 'Green', 'Blue', 'Yellow'],
]);
```

Validating responses
--------------------

[](#validating-responses)

### Validation helpers

[](#validation-helpers)

In most cases, you'll want to validate responses to your custom fields before saving them. You can do so by calling the `validateCustomFields()` function on your model:

```
$responses = [
    '1' => "John Doe",
    '2' => "Blue"
];
$survey->validateCustomFields($responses);
```

You can also pass in a `Request` object:

```
use Request;

class FooController extends Controller {

    public function index(Request $request)
    {
        $validation = $survey->validateCustomFields($request);

        // ...
    }

}
```

```

```

When using a `Request` object, the input key should be an array of values

### Implicit validation rules

[](#implicit-validation-rules)

The 5 supported field types described above automatically have the following validation rules applied to them:

- `text` : `string|max:255`
- `textarea` : `string`
- `radio` : `string|max:255|in:answers`
- `select`: `string|max:255|in:answers`
- `checkbox`: `in:0,1`

*Important: when using checkboxes, it is important you POST unchecked boxes as well, otherwise your response data may be incomplete.*

### Required fields

[](#required-fields)

Because of how common they are, required fields have native support in this package. To mark a field as required, simply set `required` to true when creating a custom field.

```
$survey->customFields()->create([
    'title' => 'Do you love Laravel?',
    'type' => 'radio',
    'answers' => ['Yes', 'YES'],
    'required' => true
]);
```

### Custom validation rules

[](#custom-validation-rules)

Along with the built in validation rules, you can apply your own rules to the any custom field. For example, if you wanted to validate a field was an integer between 1 and 10, you could do the following:

```
$survey->customFields()->create([
    'title' => 'Pick a number 1-10',
    'type' => 'text',
    'validation_rules' => 'integer|min:1|max:10'
]);
```

Remember, the `validation_rules` supports any of the [available validation rules](https://laravel.com/docs/6.x/validation#available-validation-rules) in Laravel.

### Validation Rule Sets

[](#validation-rule-sets)

-&gt; nah? In some cases, it's easier and more practical to define validation rules sets. For example, in our Survey app, if we wanted to offer a

Saving Responses
----------------

[](#saving-responses)

To store responses to custom fields, just call `saveCustomFields()` and pass in an array of values

The `saveCustomFields` function can take in a Request or array.

```
$surveyResponse->saveCustomFields(['

']);
```

If you're submitting a form request, you can easily:

```
Use App\...
$surveyResponse->saveCustomFields($request->input);
```

Querying responses
------------------

[](#querying-responses)

You can query for responses on any field by using the `WhereCustomField()` scope. The function takes in the field object and the value you're looking for. To learn more about query scopes visit [this link](https://laravel.com/docs/6.x/eloquent#query-scopes).

For example, if you wanted to find all `SurveyResponses` with a `large` T-shirt size, perform the following query:

```
Use App\Models\SurveyResponse;
Use App\Models\SurveyResponse;

$field =

SurveyResponse::WhereCustomField($field, 'large')->get();
```

Ordering
--------

[](#ordering)

You can change the order of custom fields on a model by using the `order` function. Pass in either an array or `Collection` of ids. The index position of the field represent the order position of it.

```
$survey->orderCustomFields([2, 4, 5]); // Field with id 2 will be ordered first.
```

You can also manually change the value of the `order` column:

```
$field->order = 3;
$field->save();
```

By default, custom fields are returned in ascending order when retrieved via the relation:

```
$survey->customFields()->get(); // Returned in ascending order.
```

Configuration
-------------

[](#configuration)

To publish the configuration file, run the following command:

```
php artisan vendor:publish --provider="InformaticaMobius\LaravelCustomFields\LaravelCustomFieldsServiceProvider" --tag="config"
```

The configuration file should now be published in `config/custom-fields.php`. The available options and their usage are explained inside the published file.

License
-------

[](#license)

Released under the [MIT](https://choosealicense.com/licenses/mit/) license. See [LICENSE](LICENSE.md) for more information.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance33

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~8 days

Total

3

Last Release

705d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c1b3c86abafab0e9a27f4fdaf2d22bd020ed69fd6e7bf46f9478f9503d8aaa17?d=identicon)[InformaticaMobius](/maintainers/InformaticaMobius)

---

Top Contributors

[![bastones](https://avatars.githubusercontent.com/u/845891?v=4)](https://github.com/bastones "bastones (16 commits)")[![liran-co](https://avatars.githubusercontent.com/u/5923787?v=4)](https://github.com/liran-co "liran-co (10 commits)")[![DanielCoulbourne](https://avatars.githubusercontent.com/u/429010?v=4)](https://github.com/DanielCoulbourne "DanielCoulbourne (9 commits)")[![ClintWinter](https://avatars.githubusercontent.com/u/15218914?v=4)](https://github.com/ClintWinter "ClintWinter (5 commits)")[![jaureguivictoria](https://avatars.githubusercontent.com/u/3597172?v=4)](https://github.com/jaureguivictoria "jaureguivictoria (2 commits)")[![msEmmaMays](https://avatars.githubusercontent.com/u/57109782?v=4)](https://github.com/msEmmaMays "msEmmaMays (1 commits)")[![fabiofdsantos](https://avatars.githubusercontent.com/u/8303937?v=4)](https://github.com/fabiofdsantos "fabiofdsantos (1 commits)")[![Fakirim](https://avatars.githubusercontent.com/u/60297946?v=4)](https://github.com/Fakirim "Fakirim (1 commits)")[![imanghafoori1](https://avatars.githubusercontent.com/u/6961695?v=4)](https://github.com/imanghafoori1 "imanghafoori1 (1 commits)")

---

Tags

laravelcustom fields

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[illuminate/database

The Illuminate Database package.

2.8k52.4M9.3k](/packages/illuminate-database)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[cybercog/laravel-clickhouse

ClickHouse migrations for Laravel

163166.8k](/packages/cybercog-laravel-clickhouse)

PHPackages © 2026

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