Download the PHP package watheqalshowaiter/model-fields without Composer
On this page you can find all versions of the php package watheqalshowaiter/model-fields. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download watheqalshowaiter/model-fields
More information about watheqalshowaiter/model-fields
Files in watheqalshowaiter/model-fields
Package model-fields
Short Description Get model fields fast — required, nullable, or default
License MIT
Homepage https://github.com/watheqalshowaiter/model-fields
Informations about the package model-fields
Model Fields
Quickly retrieve required, nullable, and default fields for any Laravel model. Think that's simple? You probably haven’t faced the legacy projects I have. :).
[!Note]
This is the documentation for version 3, if you want the version 1 or version 2 documentations go
V2 with this link.\ V1 with this link.
Installation
You can install the package via Composer:
We prefer --dev because you usually use it in development, not in production. If you have a use case that requires
using the package in production, then remove the --dev flag.
Optionally, if you want to publish the configuration to disable/enable model macros.
Usage
We Assume that the User model has this schema as the default.
[!IMPORTANT]
We have two ways:
- Either use the
ModelFieldsfacade.- Or use the method statically on the model. (using the magic of laravel macros).
- Or use the
model:fieldsconsole command.
Here is the sample:
That's it!
[!NOTE] To disable the macro approach, set the
enable_macrovalue to false in the publishedmodel-fields.phpconfiguration file.
Another Complex Table
Let's say the Post model has these fields
Observer and Event-Filled Fields
Fields that are automatically filled when creating by model observers, boot events, and event listeners are automatically excluded from required fields.
The package supports three patterns:
- Boot method closures:
self::creating(),self::saving() - Observer pattern:
PostObserverclass - Dispatched events:
$dispatchesEventsproperty
For example, given this model:
And more
We have the flexibility to get all fields, required fields, nullable fields, primary key, database default fields, application default fields, and default fields. You can use these methods with these results:
All fields
Nullable fields
Primary field
Database default fields
Application default fields
Default fields
More on console commands
-
All fields is the default option if you didn't specify one.
-
The package will try to find models in common places if you don't provide full namespace.
-
You can add namespaces in two ways: in two backslashes
\\or inside double quotes"". This is a laravel thing and not specific to the package. - You have 3 output formats: list, json, and table. the list is the default
Why?
The problem
I 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 tried building them. However, it was hard to figure out the required fields for testing the basic functionality since some tables have too many fields across many migration files.
The Solution
To solve this, I first created a simple facade class and a trait (which was later removed) to allow direct method usage on models for retrieving required fields. Later, I added support for older Laravel versions, as most use cases were on those versions.
So Briefly, This package is useful if:
- you want to build factories or tests for projects you didn't start from scratch.
- you are working with a legacy project and don't want to be faced with SQL errors when creating tables.
- you have so many fields in your table and want to get types of fields fast, like required, nullable, default fields.
- or any use case you find it useful.
Features
✅ Supports Laravel versions: 12, 11, 10, 9, 8, 7, and 6.
✅ Supports PHP versions: 8.4, 8.3, 8.2, 8.1, 8.0, and 7.4.
✅ Supports multiple ways of fetching fields: using console commands, or facades, or models macros.
✅ Supports SQL databases: SQLite, MySQL/MariaDB, PostgreSQL, and SQL Server.
✅ Fully automated tested with PHPUnit.
✅ Full GitHub Action CI pipeline to format code and test against all Laravel and PHP versions.
✅ Can return fields based on the dynamically added class strings (in the facade method).
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
If you have any ideas or suggestions to improve it or fix bugs, your contribution is welcome.
I encourage you to look at Issues which are the most important features that need to be added.
If you have something different, submit an issue first to discuss or report a bug, then do a pull request.
Security Vulnerabilities
If you find any security vulnerabilities don't hesitate to contact me at watheqalshowaiter[at]gmail[dot]com to fix
them.
Related Packages
- Backup Tables - Backup single or multiple database tables with ease.
- Filament Sticky Table Header - Make Filament table headers stick when scrolling for better UX.
Support this project
If this project helps you, consider supporting it on Ko-fi ☕.
Credits
License
The MIT License (MIT). Please see License File for more information.
All versions of model-fields with dependencies
illuminate/contracts Version ^12.0||^11.0||^10.0||^9.0||^8.0||^7.0||^6.0||^5.0