{"id":15002687,"url":"https://github.com/WatheqAlshowaiter/model-fields","last_synced_at":"2025-10-30T09:30:44.368Z","repository":{"id":248221602,"uuid":"828114410","full_name":"WatheqAlshowaiter/model-required-fields","owner":"WatheqAlshowaiter","description":"Get the required model fields, excluding primary keys, nullable fields, and fields with defaults.","archived":false,"fork":false,"pushed_at":"2025-02-05T15:12:15.000Z","size":209,"stargazers_count":33,"open_issues_count":5,"forks_count":3,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-02-14T00:44:57.467Z","etag":null,"topics":["laravel","laravel-package","model","mysql","php","php-package","postgresql","sql","sqlite","sqlserver","yemenopensource"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/watheqalshowaiter/model-required-fields","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/WatheqAlshowaiter.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"WatheqAlshowaiter"}},"created_at":"2024-07-13T06:52:50.000Z","updated_at":"2025-02-05T15:12:19.000Z","dependencies_parsed_at":"2024-09-28T14:20:39.760Z","dependency_job_id":"e98f2ff7-bdd4-410a-bd50-bd141f1af2d8","html_url":"https://github.com/WatheqAlshowaiter/model-required-fields","commit_stats":null,"previous_names":["watheqalshowaiter/required-fields"],"tags_count":16,"template":false,"template_full_name":"spatie/package-skeleton-laravel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WatheqAlshowaiter%2Fmodel-required-fields","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WatheqAlshowaiter%2Fmodel-required-fields/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WatheqAlshowaiter%2Fmodel-required-fields/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WatheqAlshowaiter%2Fmodel-required-fields/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WatheqAlshowaiter","download_url":"https://codeload.github.com/WatheqAlshowaiter/model-required-fields/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238950485,"owners_count":19557533,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["laravel","laravel-package","model","mysql","php","php-package","postgresql","sql","sqlite","sqlserver","yemenopensource"],"created_at":"2024-09-24T18:51:53.422Z","updated_at":"2025-10-30T09:30:44.359Z","avatar_url":"https://github.com/WatheqAlshowaiter.png","language":"PHP","funding_links":["https://github.com/sponsors/WatheqAlshowaiter"],"categories":[],"sub_categories":[],"readme":"![Package cover](./arts/package-cover.png)\n\n# Model Fields\n\n\u003c!-- shields --\u003e\n[![Required Laravel Version][ico-laravel]][link-packagist]\n[![Required PHP Version][ico-php]][link-packagist]\n[![Latest Version on Packagist][ico-version]][link-packagist]\n![GitHub Tests For Laravel Versions Action Status][ico-tests-for-laravel-versions]\n![GitHub Tests For Databases Action Status][ico-tests-for-databases]\n![GitHub Code Style Action Status][ico-code-style]\n[![Total Downloads][ico-downloads]][link-downloads]\n![GitHub Stars][ico-github-stars]\n[![StandWithPalestine][ico-palestine]][link-palestine]\n\n[ico-laravel]: https://img.shields.io/badge/Laravel-%E2%89%A56.0-ff2d20?style=flat-square\u0026logo=laravel\n\n[ico-php]: https://img.shields.io/packagist/php-v/watheqalshowaiter/model-fields?color=%238892BF\u0026style=flat-square\u0026logo=php\n\n[ico-version]: https://img.shields.io/packagist/v/watheqalshowaiter/model-fields.svg?style=flat-square\n\n[ico-downloads]: https://img.shields.io/packagist/dt/watheqalshowaiter/model-fields.svg?style=flat-square\u0026color=%23007ec6\n\n[ico-code-style]: https://img.shields.io/github/actions/workflow/status/watheqalshowaiter/model-fields/fix-php-code-style-issues.yml?branch=main\u0026label=code%20style\u0026style=flat-square\n\n[ico-tests-for-laravel-versions]: https://img.shields.io/github/actions/workflow/status/watheqalshowaiter/model-fields/tests-for-laravel-versions.yml?branch=main\u0026label=laravel%20versions%20tests\u0026style=flat-square\n\n[ico-tests-for-databases]: https://img.shields.io/github/actions/workflow/status/watheqalshowaiter/model-fields/tests-for-databases.yml?branch=main\u0026label=databases%20tests\u0026style=flat-square\n\n[ico-github-stars]: https://img.shields.io/github/stars/watheqalshowaiter/model-fields?style=flat-square\n\n[ico-palestine]: https://raw.githubusercontent.com/TheBSD/StandWithPalestine/main/badges/StandWithPalestine.svg\n\n[link-packagist]: https://packagist.org/packages/watheqalshowaiter/model-fields\n\n[link-downloads]: https://packagist.org/packages/watheqalshowaiter/model-fields/stats\n\n[link-palestine]: https://github.com/TheBSD/StandWithPalestine/blob/main/docs/README.md\n\u003c!-- ./shields --\u003e\n\nQuickly retrieve **required**, **nullable**, and **default** fields for any Laravel model. Think that's simple? You probably haven’t faced the legacy projects I have. :).\n\n\u003e [!Note]  \n\u003e This is the documentation for version 3, if you want the version 1 or version 2 documentations go  \n\u003e V2 with [this link](./v2.documentation.md).\\\n\u003e V1 with [this link](./v1.documentation.md).\n\n## Installation\n\nYou can install the package via Composer:\n\n```sh\ncomposer require watheqalshowaiter/model-fields --dev\n```\n\nWe prefer `--dev` because you usually use it in development, not in production. If you have a use case that requires\nusing the package in production, then remove the --dev flag.\n\nOptionally, if you want to publish the configuration to disable/enable model macros.\n\n```sh\nphp artisan vendor:publish --provider=\"WatheqAlshowaiter\\ModelFields\\ModelFieldsServiceProvider\" --tag=\"config\"\n```\n\n## Usage\n\nWe Assume that the `User` model has this schema as the default.\n\n```php\nSchema::create('users', function (Blueprint $table) {\n    $table-\u003eid(); // primary key\n    $table-\u003estring('name'); // required\n    $table-\u003estring('email')-\u003eunique(); // required\n    $table-\u003etimestamp('email_verified_at')-\u003enullable(); // nullable\n    $table-\u003estring('password'); // required\n    $table-\u003estring('random_number'); // default (in model attributes)\n    $table-\u003erememberToken(); // nullable\n    $table-\u003etimestamps(); // nullable\n});\n```\n\n\u003e [!IMPORTANT]  \n\u003e We have two ways:\n\u003e - Either use the `ModelFields` facade.\n\u003e - Or use the method statically on the model. (using the magic of laravel macros).\n\u003e - Or use the `model:fields` console command.\n\nHere is the sample:\n\n```php\n// Facade way\nuse WatheqAlshowaiter\\ModelFields\\Fields;\nuse App\\Models\\User;\n\nFields::model(User::class)-\u003eallFields(); // returns ['id', 'name', 'email', 'email_verified_at', 'password', 'random_number', 'remember_token', 'created_at', 'updated_at']\nFields::model(User::class)-\u003erequiredFields(); // returns ['name', 'email', 'password']\n```\n\n```php\n// Macro way\nUser::allFields(); // returns ['id', 'name', 'email', 'email_verified_at', 'password', 'random_number', 'remember_token', 'created_at', 'updated_at']\nUser::requiredFields(); // returns ['name', 'email', 'password']\n```\n\n```sh \n# console command\nphp artisan model:fields \\\\App\\\\Models\\\\User --all --format=json\nphp artisan model:fields \"App\\Models\\User\" --required --format=table\n```\n\nThat's it!\n\n\u003e [!NOTE]\n\u003e To disable the macro approach, set the `enable_macro` value to false in the published `model-fields.php`\n\u003e configuration file.\n\n### Another Complex Table\n\nLet's say the `Post` model has these fields\n\n```php\nSchema::create('posts', function (Blueprint $table) {\n    $table-\u003euuid('id')-\u003eprimary(); // primary key\n    $table-\u003eforeignId('user_id')-\u003econstrained(); // required\n    $table-\u003eforeignId('category_id')-\u003enullable(); // nullable\n    $table-\u003euuid(); // required (but will be changed later) 👇\n    $table-\u003eulid('ulid')-\u003enullable(); // nullable (but will be changed later) 👇\n    $table-\u003eboolean('active')-\u003edefault(false); // default\n    $table-\u003estring('title'); // required\n    $table-\u003ejson('description')-\u003enullable(); // nullable (but will be changed later) 👇\n    $table-\u003estring('slug')-\u003enullable()-\u003eunique(); // nullable\n    $table-\u003etimestamps(); // nullable\n    $table-\u003esoftDeletes(); // nullable\n});\n\n// later migration..\nSchema::table('posts', function(Blueprint $table){\n    $table-\u003ejson('description')-\u003enullable(false)-\u003echange(); // required\n    $table-\u003eulid('ulid')-\u003enullable(false)-\u003echange(); // required\n    $table-\u003euuid()-\u003enullable()-\u003echange(); // nullable\n});\n```\n\n```php\n// Facade way \nFields::model(Post::class)-\u003erequiredFields(); // returns ['user_id', 'ulid', 'title', 'description']\n// Macro way\nPost::requiredFields();  // returns ['user_id', 'ulid', 'title', 'description']\n```\n\n```sh\n# console command \nphp artisan model:fields App\\\\Models\\\\Post --required # or -r\n```\n\n### And more\n\nWe have the flexibility to get all fields, required fields, nullable fields, primary key, database default fields,\napplication default fields, and default fields. You can use these methods with these results:\n\n#### All fields\n\n```php\nFields::model(Post::class)-\u003eallFields();\n\n// or\nPost::allFields();\n\n// returns\n// [    'category_id', 'uuid', 'ulid', 'description',\n//      'slug', 'created_at', 'updated_at', 'deleted_at'\n// ]\n```\n\n```sh\nphp artisan model:fields App\\\\Models\\\\Post --all # or just the model without option because it is the default\n```\n\n#### Nullable fields\n\n```php\nFields::model(Post::class)-\u003enullableFields();\n\n//or\nPost::nullableFields();\n\n// returns\n// [\n//     'category_id', 'uuid', 'slug',\n//     'created_at', 'updated_at', 'deleted_at'\n// ]\n```\n\n```sh\n# console command\nphp artisan model:fields App\\\\Models\\\\Post --nullable    # or -N\n```\n\n#### Primary field\n\n```php\nFields::model(Post::class)-\u003eprimaryField();\n\n// or\nPost::primaryField();\n\n// returns ['id']\n```\n\n```sh\n# console command\nphp artisan model:fields User --primary     # or -p\n```\n\n#### Database default fields\n\n```php\nFields::model(Post::class)-\u003edatabaseDefaultFields();\n\n//or \nPost::databaseDefaultFields();\n\n// returns ['active']\n```\n\n```sh\n# console command\nphp artisan model:fields User --db-default  # or -D\n```\n\n#### Application default fields\n\n```php\nFields::model(Post::class)-\u003eapplicationDefaultFields();\n\n//or \nPost::applicationDefaultFields();\n\n// If there is default attributes in the model\nclass Post extends Model\n{\n    protected $attributes = [\n        'title' =\u003e 'default title', \n        'description' =\u003e 'default description',\n    ];\n}\n\n// returns\n// [\n//     'title', 'description',\n// ]\n```\n\n```sh\n#console command\nphp artisan model:fields User --app-default # or -A\n```\n\n#### Default fields\n\n```php\nFields::model(Post::class)-\u003edefaultFields();\n\n//or \nPost::defaultFields();\n\n// This will combine application and database defaults \nclass Post extends Model\n{\n    protected $attributes = [\n        'title' =\u003e 'default title', \n        'description' =\u003e 'default description',\n    ];\n}\n\n// returns\n// [\n//    'active', 'title', 'description',\n// ]\n```\n\n```sh\n#console command\nphp artisan model:fields User --default     # or -d\n```\n\n### More on console commands\n\n- All fields is the default option if you didn't specify one.\n\n```sh\nphp artisan model:fields \\\\App\\\\Models\\\\Post # will result all fields\n```\n\n- The package will try to find models in common places if you don't provide full namespace.\n\n```sh\nphp artisan model:fields User # It will try to find the model in `App\\Models\\User` or `App\\User` namespaces\n```\n\n- You can add namespaces in two ways: in two backslashes `\\\\` or inside double quotes `\"\"`. This is a laravel thing and\n  not specific to the package.\n\n```sh\nphp artisan model:fields \\\\Modules\\\\Order\\\\src\\\\Models\\\\Order\n# or \nphp artisan model:fields \"Modules\\Order\\src\\Models\\Order\"\n```\n\n- You have 3 output formats: list, json and table. the list is the default\n\n```sh\nphp artisan model:fields User --format=json\nphp artisan model:fields User --format=table\nphp artisan model:fields User --format=list  # default\n```\n\n## Why?\n\n### The problem\n\nI wanted to add tests to a legacy project that didn't have any. I wanted to add tests but couldn't find a factory, so I\ntried building them. However, it was hard to figure out the required fields for testing the basic functionality since\nsome tables have too many fields across many migration files.\n\n### The Solution\n\nTo solve this, I first created a simple facade class and a trait (which was later removed) to allow direct method usage\non models for retrieving required fields. Later, I added support for older Laravel versions, as most use cases were on\nthose versions.\n\nSo Briefly, This package is useful if:\n\n- you want to build factories or tests for projects you didn't start from scratch.\n- you are working with a legacy project and don't want to be faced with SQL errors when creating tables.\n- you have so many fields in your table and want to get types of fields fast, like required, nullable, default fields.\n- or any use case you find it useful.\n\n## Features\n\n✅ Supports Laravel versions: 12, 11, 10, 9, 8, 7, and 6.\n\n✅ Supports PHP versions: 8.4, 8.3, 8.2, 8.1, 8.0, and 7.4.\n\n✅ Supports multiple ways of fetching fields: using console commands, or facades, or models macros.\n\n✅ Supports SQL databases: SQLite, MySQL/MariaDB, PostgreSQL, and SQL Server.\n\n✅ Fully automated tested with PHPUnit.\n\n✅ Full GitHub Action CI pipeline to format code and test against all Laravel and PHP versions.\n\n✅ Can return fields based on the dynamically added class strings (in the facade method).\n\n## Testing\n\n```sh\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nIf you have any ideas or suggestions to improve it or fix bugs, your contribution is welcome.\n\nI encourage you to look at [Issues](https://github.com/WatheqAlshowaiter/model-fields/issues) which are the\nmost important features that need to be added.\n\nIf you have something different, submit an issue first to discuss or report a bug, then do a pull request.\n\n## Security Vulnerabilities\n\nIf you find any security vulnerabilities don't hesitate to contact me at `watheqalshowaiter[at]gmail[dot]com` to fix\nthem.\n\n## Related Packages\n\n- **[Backup Tables](https://github.com/WatheqAlshowaiter/backup-tables)** - Backup single or multiple database tables with ease.\n- **[Filament Sticky Table Header](https://github.com/WatheqAlshowaiter/filament-sticky-table-header)** - Make Filament table headers stick when scrolling for better UX.\n\n## Credits\n\n- [Watheq Alshowaiter](https://github.com/WatheqAlshowaiter)\n\n- [All Contributors](https://github.com/WatheqAlshowaiter/model-fields/graphs/contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FWatheqAlshowaiter%2Fmodel-fields","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FWatheqAlshowaiter%2Fmodel-fields","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FWatheqAlshowaiter%2Fmodel-fields/lists"}