Skip to content

[9.x] Fixes default attribute value when using enums on model:show#43360

Merged
taylorotwell merged 2 commits into
laravel:9.xfrom
josecl:fixes-default-values-for-artisan-model-show-command
Jul 22, 2022
Merged

[9.x] Fixes default attribute value when using enums on model:show#43360
taylorotwell merged 2 commits into
laravel:9.xfrom
josecl:fixes-default-values-for-artisan-model-show-command

Conversation

@josecl

@josecl josecl commented Jul 22, 2022

Copy link
Copy Markdown
Contributor

Problem

Having an UnitEnum or BackedEnum as default attribute value in a Model, like this:

    protected $casts = [
        'status' => InvoiceStatus::class,
    ];

    protected $attributes = [
        'status' => InvoiceStatus::I_LOVE_LARAVEL,
    ];

Triggers an error on model:show command:

ERROR: Object of class App\Enums\InvoiceStatus could not be converted to string

Fix

This PR fixes it displaying the value of enums name attribute:

 status fillable ................................... integer / App\Enums\InvoiceStatus
  ⇂ default: I_LOVE_LARAVEL

@driesvints
driesvints requested a review from jessarcher July 22, 2022 09:23
@taylorotwell
taylorotwell merged commit 0f53f65 into laravel:9.x Jul 22, 2022
@jessarcher

Copy link
Copy Markdown
Member

Hey @josecl,

Thanks for this! I think the error will still occur with the --json flag, so it might be better to move this change to the getColumnDefault() method to fix it everywhere. I'll work up a PR now.

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