laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.66k stars 11.04k forks source link

[11.x] Extract `ShowModelCommand` functionality to separate class #53541

Closed cosmastech closed 1 week ago

cosmastech commented 1 week ago

The ShowModelCommand provides incredibly valuable information. It seems like third-party packages would be able to benefit from a unified way to retrieve model data. For instance, the laravel-ide-helper package has very similar logic. Larastan (which I don't assume this would replace, but pointing it out). Laravel Blueprint does a lot of this sort of parsing as well.

My thought is that this could be useful for a package that generates DTOs for models, thinking in terms of the idea of creating frozen models.


This also adds a fairly simple test for the console command output which previously was untested.

It would be nice if this command also returns the Model's $builder class and $collectionClass. There could also be a number of docblock generics added to arrays in these functions. I figured this was already a pretty large PR, so that could follow up (whether this is accepted or not).

Example Output ```php $modelDetails = app(ModelInspector::class)->inspect(User::class); /* [ "class" => "App\Models\User", "database" => "mysql", "table" => "users", "policy" => null, "attributes" => Illuminate\Support\Collection {#6567 all: [ [ "name" => "id", "type" => "bigint unsigned", "increments" => true, "nullable" => false, "default" => null, "unique" => true, "fillable" => false, "hidden" => false, "appended" => null, "cast" => "int", ], [ "name" => "first_name", "type" => "varchar(255)", "increments" => false, "nullable" => false, "default" => null, "unique" => false, "fillable" => true, "hidden" => false, "appended" => null, "cast" => null, ], [ "name" => "last_name", "type" => "varchar(255)", "increments" => false, "nullable" => false, "default" => null, "unique" => false, "fillable" => true, "hidden" => false, "appended" => null, "cast" => null, ], [ "name" => "email", "type" => "varchar(255)", "increments" => false, "nullable" => false, "default" => null, "unique" => true, "fillable" => true, "hidden" => false, "appended" => null, "cast" => null, ], [ "name" => "profile_photo_url", "type" => "text", "increments" => false, "nullable" => true, "default" => null, "unique" => false, "fillable" => true, "hidden" => false, "appended" => null, "cast" => null, ], [ "name" => "created_at", "type" => "timestamp", "increments" => false, "nullable" => true, "default" => null, "unique" => false, "fillable" => false, "hidden" => false, "appended" => null, "cast" => "datetime", ], [ "name" => "updated_at", "type" => "timestamp", "increments" => false, "nullable" => true, "default" => null, "unique" => false, "fillable" => false, "hidden" => false, "appended" => null, "cast" => "datetime", ], ], }, "relations" => Illuminate\Support\Collection {#6653 all: [ [ "name" => "tokens", "type" => "MorphMany", "related" => "App\Models\PersonalAccessToken", ], [ "name" => "notifications", "type" => "MorphMany", "related" => "Illuminate\Notifications\DatabaseNotification", ], ], }, "events" => Illuminate\Support\Collection {#6650 all: [], }, "observers" => Illuminate\Support\Collection {#6656 all: [], }, "collection" => "Illuminate\Database\Eloquent\Collection", "builder" => "Illuminate\Database\Eloquent\Builder", ] */ ```
github-actions[bot] commented 1 week ago

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.