Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 15 commits into from
Nov 18, 2024

Conversation

cosmastech
Copy link
Contributor

@cosmastech cosmastech commented Nov 16, 2024

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
$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",
  ]
 */

Copy link

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.

@cosmastech cosmastech marked this pull request as ready for review November 17, 2024 00:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants