Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
05492d3
refactor: replace ThanksCommand with ModelFieldsCommand
WatheqAlshowaiter Sep 19, 2025
fd6f814
fix: $this->info in command in older laravel versions
WatheqAlshowaiter Sep 19, 2025
354e7b4
Fix styling
WatheqAlshowaiter Sep 19, 2025
9d2f699
fix(command): improve Laravel version compatibility for output
WatheqAlshowaiter Sep 19, 2025
a0cb94a
Merge branch 'model-fields-command' of github.com:WatheqAlshowaiter/m…
WatheqAlshowaiter Sep 19, 2025
635d52f
debug(command): Laravel version compatibility for output
WatheqAlshowaiter Sep 19, 2025
61ff0f5
Fix styling
WatheqAlshowaiter Sep 19, 2025
4025a1c
refactor(tests): use BufferedOutput for stable command output capture
WatheqAlshowaiter Sep 19, 2025
b491ab0
Merge branch 'model-fields-command' of github.com:WatheqAlshowaiter/m…
WatheqAlshowaiter Sep 19, 2025
c19052b
debug: remoe type hint for compatibility
WatheqAlshowaiter Sep 19, 2025
eda9bca
Fix styling
WatheqAlshowaiter Sep 19, 2025
7d84e54
test: refactor command tests to use laravel testing methods for backw…
WatheqAlshowaiter Sep 20, 2025
cc37941
Merge branch 'model-fields-command' of github.com:WatheqAlshowaiter/m…
WatheqAlshowaiter Sep 20, 2025
e5972de
Fix styling
WatheqAlshowaiter Sep 20, 2025
e5a097e
test: refactor command tests to use expectsOutput instead of expectsO…
WatheqAlshowaiter Sep 20, 2025
eed136e
Merge branch 'model-fields-command' of github.com:WatheqAlshowaiter/m…
WatheqAlshowaiter Sep 20, 2025
6abf344
Fix styling
WatheqAlshowaiter Sep 20, 2025
7ef197f
test: update console command assertions for laravel compatibility
WatheqAlshowaiter Sep 20, 2025
2babe9e
Fix styling
WatheqAlshowaiter Sep 20, 2025
e63334d
test: remove doesntExpectOutput for laravel 7 compatibility
WatheqAlshowaiter Sep 20, 2025
183006b
Merge branch 'model-fields-command' of github.com:WatheqAlshowaiter/m…
WatheqAlshowaiter Sep 20, 2025
c1a5fd9
test: improve console test compatibility and consistency
WatheqAlshowaiter Sep 20, 2025
074969b
Fix styling
WatheqAlshowaiter Sep 20, 2025
4f37265
Merge pull request #28 from WatheqAlshowaiter/model-fields-command
WatheqAlshowaiter Sep 20, 2025
dd9c336
Update CHANGELOG
WatheqAlshowaiter Sep 20, 2025
57e765a
docs: add comprehensive console command documentation
WatheqAlshowaiter Sep 20, 2025
2a2b587
Merge pull request #29 from WatheqAlshowaiter/fields-readme
WatheqAlshowaiter Sep 20, 2025
31392a9
Update CHANGELOG
WatheqAlshowaiter Sep 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

All notable changes to `model-required-fields` will be documented in this file.

## 3.1.1 - 2025-09-20

### What's Changed

* Add documentation for console commands

**Full Changelog**: https://github.com/WatheqAlshowaiter/model-fields/compare/3.1.0...3.1.1

## 3.1.0 - 2025-09-20

### What's Changed

* feat: add askToStarRepository command by @WatheqAlshowaiter in https://github.com/WatheqAlshowaiter/model-fields/pull/26

**Full Changelog**: https://github.com/WatheqAlshowaiter/model-fields/compare/3.0.2...3.1.0

## 3.0.2 - 2025-09-19

Fix banner when install
Expand Down
103 changes: 91 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ 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
> V1 with [this link](./v1.documentation.md).
> V2 with [this link](./v2.documentation.md).
> V2 with [this link](./v2.documentation.md).\
> V1 with [this link](./v1.documentation.md).

## Installation

Expand Down Expand Up @@ -84,9 +84,9 @@ Schema::create('users', function (Blueprint $table) {
> We have two ways:
> - Either use the `ModelFields` facade.
> - Or use the method statically on the model. (using the magic of laravel macros).
> - Or use the `model:fields` console command.

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.
Here is the sample:

```php
// Facade way
Expand All @@ -103,9 +103,15 @@ User::allFields(); // returns ['id', 'name', 'email', 'email_verified_at', 'pass
User::requiredFields(); // returns ['name', 'email', 'password']
```

```sh
# console command
php artisan model:fields \\App\\Models\\User --all --format=json
php artisan model:fields "App\Models\User" --required --format=table
```

That's it!

> [!NOTE]
> [!NOTE]
> To disable the macro approach, set the `enable_macro` value to false in the published `model-fields.php`
> configuration file.

Expand Down Expand Up @@ -143,13 +149,19 @@ Fields::model(Post::class)->requiredFields(); // returns ['user_id', 'ulid', 'ti
Post::requiredFields(); // returns ['user_id', 'ulid', 'title', 'description']
```

```sh
# console command
php artisan model:fields App\\Models\\Post --required # or -r
```

### 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

```php
// All fields
Fields::model(Post::class)->allFields();

// or
Expand All @@ -161,8 +173,13 @@ Post::allFields();
// ]
```

```sh
php artisan model:fields App\\Models\\Post --all # or just the model without option because it is the default
```

#### Nullable fields

```php
// Nullable fields
Fields::model(Post::class)->nullableFields();

//or
Expand All @@ -175,8 +192,14 @@ Post::nullableFields();
// ]
```

```sh
# console command
php artisan model:fields App\\Models\\Post --nullable # or -N
```

#### Primary field

```php
// Primary field
Fields::model(Post::class)->primaryField();

// or
Expand All @@ -185,8 +208,14 @@ Post::primaryField();
// returns ['id']
```

```sh
# console command
php artisan model:fields User --primary # or -p
```

#### Database default fields

```php
// Database default fields
Fields::model(Post::class)->databaseDefaultFields();

//or
Expand All @@ -195,8 +224,14 @@ Post::databaseDefaultFields();
// returns ['active']
```

```sh
# console command
php artisan model:fields User --db-default # or -D
```

#### Application default fields

```php
// Application default fields
Fields::model(Post::class)->applicationDefaultFields();

//or
Expand All @@ -217,8 +252,14 @@ class Post extends Model
// ]
```

```sh
#console command
php artisan model:fields User --app-default # or -A
```

#### Default fields

```php
// Default fields
Fields::model(Post::class)->defaultFields();

//or
Expand All @@ -239,13 +280,49 @@ class Post extends Model
// ]
```

```sh
#console command
php artisan model:fields User --default # or -d
```

### More on console commands

- All fields is the default option if you didn't specify one.

```sh
php artisan model:fields \\App\\Models\\Post # will result all fields
```

- The package will try to find models in common places if you don't provide full namespace.

```sh
php artisan model:fields User # It will try to find the model in `App\Models\User` or `App\User` namespaces
```

- 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.

```sh
php artisan model:fields \\Modules\\Order\\src\\Models\\Order
# or
php artisan model:fields "Modules\Order\src\Models\Order"
```

- You have 3 output formats: list, json and table. the list is the default

```sh
php artisan model:fields User --format=json
php artisan model:fields User --format=table
php artisan model:fields User --format=list # 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.
some tables have too many fields across many migration files.

### The Solution

Expand All @@ -266,6 +343,8 @@ So Briefly, This package is useful if:

✅ 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.
Expand Down
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@
"@composer run build",
"@php vendor/bin/testbench serve"
],
"test": "vendor/bin/phpunit",
"post-autoload-dump": [
"@php -r \"class_exists('WatheqAlshowaiter\\\\ModelFields\\\\Console\\\\ThanksCommand') && WatheqAlshowaiter\\\\ModelFields\\\\Console\\\\ThanksCommand::show();\""
]
"test": "vendor/bin/phpunit"
},
"config": {
"sort-packages": true,
Expand Down
Loading