Get the required model fields, excluding primary keys, nullable fields, and fields with defaults. In other words, get the minimal required fields necessary to create the model without causing a database error.
Note
This is the documentation for version 2, if you want the version 1 documentation go with this link
You can install the package via Composer:
composer require watheqalshowaiter/model-required-fields --dev
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.
php artisan vendor:publish --provider="WatheqAlshowaiter\ModelRequiredFields\ModelRequiredFieldsServiceProvider" --tag="config"
We Assume that the User
model has this schema as the default.
Schema::create('users', function (Blueprint $table) {
$table->id(); // primary key
$table->string('name'); // required
$table->string('email')->unique(); // required
$table->timestamp('email_verified_at')->nullable(); // nullable
$table->string('password'); // required
$table->string('random_number'); // default (in model attributes)
$table->rememberToken(); // nullable
$table->timestamps(); // nullable
});
Important
We have two ways:
- Either use the
ModelFields
facade. - Or use the method statically on the model. (using the magic of laravel macros).
We will explain the macro way in two examples, and the other will be only using the facade way and all the methods in both ways are the same.
// Facade way
use WatheqAlshowaiter\ModelRequiredFields\ModelFields;
use App\Models\User;
ModelFields::model(User::class)->getRequiredFields(); // returns ['name', 'email', 'password']
// Macro way
User::getRequiredFields(); // returns ['name', 'email', 'password']
That's it!
Note
To disable the macro approach, set the enable_macro
value to false in the published model-required-fields.php
configuration file.
Let's say the Post
model has these fields
Schema::create('posts', function (Blueprint $table) {
$table->uuid('id')->primary(); // primary key
$table->foreignId('user_id')->constrained(); // required
$table->foreignId('category_id')->nullable(); // nullable
$table->uuid(); // required (but will be changed later) π
$table->ulid('ulid')->nullable(); // nullable (but will be changed later) π
$table->boolean('active')->default(false); // default
$table->string('title'); // required
$table->json('description')->nullable(); // nullable (but will be changed later) π
$table->string('slug')->nullable()->unique(); // nullable
$table->timestamps(); // nullable
$table->softDeletes(); // nullable
});
// later migration..
Schema::table('posts', function(Blueprint $table){
$table->json('description')->nullable(false)->change(); // required
$table->ulid('ulid')->nullable(false)->change(); // required
$table->uuid()->nullable()->change(); // nullable
});
// Facade way
ModelFields::model(Post::class)->getRequiredFields(); // returns ['user_id', 'ulid', 'title', 'description']
// Macro way
Post::getRequiredFields(); // returns ['user_id', 'ulid', 'title', 'description']
We have the flexibility to get required fields with nullables, defaults, primary keys, and a mix of them or return all fields. You can use these methods with these results:
// The default parameters only required fields
ModelFields::model(Post::class)
->getRequiredFields(
$withNullables = false,
$withDefaults = false,
$withPrimaryKey = false
);
// or
ModelFields::model(Post::class)->getRequiredFields();
// returns ['user_id', 'ulid', 'title', 'description']
// get required fields with nullables
ModelFields::model(Post::class)
->getRequiredFields(
$withNullables = true,
$withDefaults = false,
$withPrimaryKey = false
);
// or
ModelFields::model(Post::class)
->getRequiredFields(
$withNullables = true
);
// or
ModelFields::model(Post::class)
->getRequiredFields(true);
// or
ModelFields::model(Post::class)
->getRequiredFieldsWithNullables();
// returns
// [
// 'user_id', 'category_id', 'uuid', 'ulid', 'title', 'description', 'slug',
// 'created_at', 'updated_at', 'deleted_at'
// ]
// get required fields with defaults
ModelFields::model(Post::class)
->getRequiredFields(
$withNullables = false,
$withDefaults = true,
$withPrimaryKey = false
);
// or
ModelFields::model(Post::class)
->getRequiredFieldsWithDefaults();
// returns ['user_id', 'ulid', 'active', 'title', 'description']
// get required fields with primary key
ModelFields::model(Post::class)
->getRequiredFields(
$withNullables = false,
$withDefaults = false,
$withPrimaryKey = true
);
// or
ModelFields::model(Post::class)
->getRequiredFieldsWithPrimaryKey();
// returns ['id', 'user_id', 'ulid', 'title', 'description']
// get required fields with nullables and defaults
ModelFields::model(Post::class)
->getRequiredFields(
$withNullables = true,
$withDefaults = true,
$withPrimaryKey = false
);
// or
ModelFields::model(Post::class)
->getRequiredFieldsWithNullablesAndDefaults();
// returns
// [
// 'user_id', 'category_id', 'uuid', 'ulid', 'active', 'title', 'description', 'slug',
// 'created_at', 'updated_at', 'deleted_at'
// ]
// get required fields with nullables and primary key
ModelFields::model(Post::class)
->getRequiredFields(
$withNullables = true,
$withDefaults = false,
$withPrimaryKey = true
);
// or
ModelFields::model(Post::class)
->getRequiredFieldsWithNullablesAndPrimaryKey();
// returns
// [
// 'id', 'user_id', 'category_id', 'uuid', 'ulid', 'title', 'description', 'slug',
// 'created_at', 'updated_at', 'deleted_at'
// ]
// get required fields with defaults and primary key
ModelFields::model(Post::class)
->getRequiredFields(
$withNullables = false,
$withDefaults = true,
$withPrimaryKey = true
);
// or
ModelFields::model(Post::class)
->getRequiredFieldsWithDefaultsAndPrimaryKey();
// returns ['id', 'user_id', 'ulid', 'active', 'title', 'description']
// get required fields with nullables, defaults and primary key
ModelFields::model(Post::class)
->getRequiredFields(
$withNullables = true,
$withDefaults = true,
$withPrimaryKey = true
);
// or
ModelFields::model(Post::class)
->getAllFields();
// returns
// [
// 'id', 'user_id', 'category_id', 'uuid', 'ulid', 'active', 'title', 'description',
// 'slug', 'created_at', 'updated_at', 'deleted_at'
// ]
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.
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 the required fields fast.
- or any use case you find it useful.
β 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 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).
composer test
Please see CHANGELOG for more information on what has changed recently.
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.
If you find any security vulnerabilities don't hesitate to contact me at watheqalshowaiter[at]gmail[dot]com
to fix
them.
The MIT License (MIT). Please see License File for more information.