{"id":41236119,"url":"https://github.com/givebutter/laravel-custom-fields","last_synced_at":"2026-01-23T01:04:02.486Z","repository":{"id":36529047,"uuid":"205987415","full_name":"givebutter/laravel-custom-fields","owner":"givebutter","description":"Add custom fields to your Laravel models.","archived":false,"fork":false,"pushed_at":"2025-08-31T18:55:22.000Z","size":145,"stargazers_count":173,"open_issues_count":4,"forks_count":33,"subscribers_count":27,"default_branch":"master","last_synced_at":"2025-10-03T06:58:49.750Z","etag":null,"topics":["custom-fields","laravel","survey"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/givebutter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2019-09-03T04:13:37.000Z","updated_at":"2025-09-22T17:17:19.000Z","dependencies_parsed_at":"2025-08-31T20:31:04.252Z","dependency_job_id":"48128041-fe29-4b60-9518-d4043e99ad26","html_url":"https://github.com/givebutter/laravel-custom-fields","commit_stats":{"total_commits":86,"total_committers":10,"mean_commits":8.6,"dds":0.5232558139534884,"last_synced_commit":"0e21d258a41dfddbcde4b9e84e465bfc7d5884cc"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/givebutter/laravel-custom-fields","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/givebutter%2Flaravel-custom-fields","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/givebutter%2Flaravel-custom-fields/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/givebutter%2Flaravel-custom-fields/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/givebutter%2Flaravel-custom-fields/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/givebutter","download_url":"https://codeload.github.com/givebutter/laravel-custom-fields/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/givebutter%2Flaravel-custom-fields/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28676495,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-23T01:00:35.747Z","status":"ssl_error","status_checked_at":"2026-01-23T01:00:19.529Z","response_time":144,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["custom-fields","laravel","survey"],"created_at":"2026-01-23T01:04:01.813Z","updated_at":"2026-01-23T01:04:02.472Z","avatar_url":"https://github.com/givebutter.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Custom Fields\n\n\u003e ⚠️ Warning: these docs are incomplete and may reference unsupported functions. This is a pre-release version that is not yet ready for production use.\n\nLaravel Custom Fields is a package that allows you to add custom fields to any Laravel model and associate responses to those fields with other models.\n\n[![Latest Stable Version](https://poser.pugx.org/givebutter/laravel-custom-fields/v/stable)](https://packagist.org/packages/givebutter/laravel-custom-fields) [![Total Downloads](https://poser.pugx.org/givebutter/laravel-custom-fields/downloads)](https://packagist.org/packages/givebutter/laravel-custom-fields) [![License](https://poser.pugx.org/givebutter/laravel-custom-fields/license)](https://packagist.org/packages/givebutter/laravel-custom-fields)\n\n## Installation\n\nTo get started, add the `givebutter/laravel-custom-fields` package to your `composer.json` file and update your dependencies:\n\n```bash\ncomposer require givebutter/laravel-custom-fields\n```\n\nPublish the migration:\n```bash\nphp artisan vendor:publish --provider=\"Givebutter\\LaravelCustomFields\\LaravelCustomFieldsServiceProvider\" --tag=\"migrations\"\n```\n\nRun the migration:\n```bash\nphp artisan migrate\n```\n\n*You can customize the table names using configuration options. More on that later.*\n\n## An example - Survey App\nFor 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`.\n\n## Preparing your models\n\n### Adding custom fields\n\nTo add basic custom field support, simply add the `HasCustomFields` trait at the top of your model:\n\n```php\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Givebutter\\LaravelCustomFields\\Traits\\HasCustomFields;\n\nclass Survey extends Model\n{\n    use HasCustomFields;\n\n    // ...\n}\n```\n\n### Adding custom field responses\n\nNext, we add support to store custom field responses. We'll simply pull in the `HasCustomFieldResponses` trait at the top of our `SurveyResponse` model.\n\n```php\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Givebutter\\LaravelCustomFields\\Traits\\HasCustomFieldResponses;\n\nclass SurveyResponse extends Model\n{\n    use HasCustomFieldResponses;\n\n    // ...\n}\n```\n\n## Basic usage\n\n### Creating fields\n\nYou can add a field to a model like this:\n\n```php\n$survey-\u003ecustomFields()-\u003ecreate([\n    'title' =\u003e 'What is your name?',\n    'type' =\u003e 'text'\n]);\n```\n\nEach field can contain the following: (More on these later)\n\n`title` : The title / question of your custom field.\n`description` :  The description of your field. Useful for providing more context to user filling out fields.\n`type` :  The type of field you're creating. Available types are outlined in the next section.\n`required` :  A boolean representing whether a field is required or not.\n`answers` : An array of acceptable values for fields that have user-selection. \n\n### Creating field responses\n\nTo store a response on a field, you can do this:\n\n```php\n$field-\u003eresponses()-\u003ecreate([\n    'value' =\u003e 'John Doe'\n]);\n```\n\n### Retrieving fields\n\nTo retrieve custom fields, use the `customFields` relation:\n\n```php\n$survey-\u003ecustomFields()-\u003eget();\n```\n\n### Retrieving field responses\n\nTo retrieve the responses on a field,  use the `responses()` relation:\n\n```php\n$field-\u003eresponses()-\u003eget();\n```\n\n## Custom field types\nCustom fields may be any of 5 types:\n\n - `text` : Free entry fields which are stored as strings. Use these for simple inputs as they have a max-length of 255 characters.\n - `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.\n - `radio` : These are multi-select fields, which require you to pass an `answers` property.*\n - `select` : These are multi-select fields, which require you to pass an `answers` property.*\n - `checkbox` : Boolean fields.\n\nIn 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.\n\n*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:\n\n ```php\n $survey-\u003ecustomFields()-\u003ecreate([\n    'title' =\u003e 'What is your favorite color?',\n    'type' =\u003e 'select',\n    'answers' =\u003e ['Red', 'Green', 'Blue', 'Yellow'], \n]);\n```\n\n## Validating responses\n\n\n### Validation helpers\n\nIn 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:\n\n```php\n\n$responses = [\n    '1' =\u003e \"John Doe\",\n    '2' =\u003e \"Blue\"\n];\n$survey-\u003evalidateCustomFields($responses);\n```\n\nYou can also pass in a `Request` object:\n\n```php\nuse Request;\n\nclass FooController extends Controller {\n\n    public function index(Request $request) \n    {\n        $validation = $survey-\u003evalidateCustomFields($request);\n\n        // ...\n    }\n\n}\n```\n\n```html\n\u003cform\u003e\n    \u003cinput type=\"text\" name=\"custom_fields[]\" /\u003e\n\u003c/form\u003e\n```\nWhen using a `Request` object, the input key should be an array of values \n\n\n\n### Implicit validation rules\n\nThe 5 supported field types described above automatically have the following validation rules applied to them:\n\n - `text` : `string|max:255`\n - `textarea` : `string`\n - `radio` : `string|max:255|in:answers`\n - `select`: `string|max:255|in:answers`\n - `checkbox`: `in:0,1`\n\n*Important: when using checkboxes, it is important you POST unchecked boxes as well, otherwise your response data may be incomplete.*\n\n### Required fields\n\nBecause 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.\n\n```php\n$survey-\u003ecustomFields()-\u003ecreate([\n    'title' =\u003e 'Do you love Laravel?',\n    'type' =\u003e 'radio',\n    'answers' =\u003e ['Yes', 'YES'],\n    'required' =\u003e true\n]);\n```\n\n### Custom validation rules\n\nAlong 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:\n\n```php\n$survey-\u003ecustomFields()-\u003ecreate([\n    'title' =\u003e 'Pick a number 1-10',\n    'type' =\u003e 'text',\n    'validation_rules' =\u003e 'integer|min:1|max:10'\n]);\n```\n\nRemember, the `validation_rules` supports any of the [available validation rules](https://laravel.com/docs/6.x/validation#available-validation-rules) in Laravel.\n\n\n### Validation Rule Sets\n-\u003e nah?\nIn 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 \n\n## Saving Responses\n\nTo store responses to custom fields, just call `saveCustomFields()` and pass in an array of values\n\nThe `saveCustomFields` function can take in a Request or array.\n\n```php\n$surveyResponse-\u003esaveCustomFields(['\n   \n']);\n```\n\nIf you're submitting a form request, you can easily:\n\n```php\nUse App\\...\n$surveyResponse-\u003esaveCustomFields($request-\u003einput);\n```\n\n## Querying responses\n\nYou 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).\n\nFor example, if you wanted to find all `SurveyResponses` with a `large` T-shirt size, perform the following query:\n\n```php\nUse App\\Models\\SurveyResponse;\nUse App\\Models\\SurveyResponse;\n\n$field = \n\nSurveyResponse::WhereCustomField($field, 'large')-\u003eget();\n```\n\n## Ordering\n\nYou 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. \n\n```php\n$survey-\u003eorderCustomFields([2, 4, 5]); // Field with id 2 will be ordered first.\n```\n\nYou can also manually change the value of the `order` column:\n\n```php\n$field-\u003eorder = 3; \n$field-\u003esave();\n```\n\nBy default, custom fields are returned in ascending order when retrieved via the relation:\n```php\n$survey-\u003ecustomFields()-\u003eget(); // Returned in ascending order.\n```\n\n## Configuration\n\nTo publish the configuration file, run the following command:\n```bash\nphp artisan vendor:publish --provider=\"Givebutter\\LaravelCustomFields\\LaravelCustomFieldsServiceProvider\" --tag=\"custom-fields-config\"\n```\n\nThe configuration file should now be published in `config/custom-fields.php`. The available options and their usage are explained inside the published file.\n\n## License\nReleased under the [MIT](https://choosealicense.com/licenses/mit/) license. See [LICENSE](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgivebutter%2Flaravel-custom-fields","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgivebutter%2Flaravel-custom-fields","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgivebutter%2Flaravel-custom-fields/lists"}