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

$table->enum() fails with model events like booted() static::creating #53698

Closed
mauricekindermann opened this issue Nov 28, 2024 · 5 comments
Closed

Comments

@mauricekindermann
Copy link

Laravel Version

11.27

PHP Version

8.2

Database Driver & Version

10.5.26-MariaDB

Description

Create an enum migration

$table->enum('week_starts_on', [Carbon::SUNDAY, Carbon::SATURDAY, Carbon::MONDAY])->default(Carbon::SUNDAY);

Then, in your model, try and set the value.

class CalendarSetting extends Model
{
    protected static function booted()
    {
        static::creating(function ($calendarSetting) {
            $calendarSetting->week_starts_on = Carbon::MONDAY;
        });
    }

I tried a dozen different ways of doing this, including hard coded 0/1 values. Nothing worked

The only way I got it working was by using an integer

$table->integer('week_starts_on')->default(Carbon::SUNDAY);

Steps To Reproduce

php artisan make:model CalendarSetting -m;

        Schema::create('calendar_settings', function (Blueprint $table) {
            $table->id();
            $table->enum('week_starts_on', [Carbon::SUNDAY, Carbon::SATURDAY, Carbon::MONDAY])->default(Carbon::SUNDAY);
        });
class CalendarSetting extends Model
{
    protected $guarded = [];

    protected static function booted()
    {
        static::creating(function ($calendarSetting) {
            
            $calendarSetting->week_starts_on = 1;
        });
    }
}

Then use tinker to firstOrCreate a calendar setting.

week_starts_on will always be 0, no matter what you feed into the static::creating

@Jacobs63
Copy link
Contributor

Jacobs63 commented Nov 28, 2024

Hello,

Firstly, I assume your use-case is setting a default value, which should be done via $attributes:

    protected $attributes = [
        'week_starts_on' => (string) Carbon::SUNDAY,
    ];

Secondly, MariaDB does not support enum of integers, meaning the enum you've actually created is enum('0', '6', '1') - see docs.

E.g. creating it via ->week_starts_on = '1' should work.

You could also try casting the attribute as a string via $casts as well, which would then also support your Carbon constants usage:

    protected $attributes = [
        'week_starts_on' => 'string',
    ];

@mansoorkhan96
Copy link

This is happening on MySQL 8.0.33 as well. The enum is created with string values in MySQL as well, hence same issue.

@Jacobs63 I think its not important to set default value from Laravel Model attributes. we can still set the default value from table schema itself. which is what am testing at the moment and the result is same. I have default set to SUNDAY i.e '0' and storing '1' from static::creating works i.e. overrides the default schema value.

@crynobone
Copy link
Member

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@Jacobs63
Copy link
Contributor

Jacobs63 commented Dec 3, 2024

This is happening on MySQL 8.0.33 as well. The enum is created with string values in MySQL as well, hence same issue.

@Jacobs63 I think its not important to set default value from Laravel Model attributes. we can still set the default value from table schema itself. which is what am testing at the moment and the result is same. I have default set to SUNDAY i.e '0' and storing '1' from static::creating works i.e. overrides the default schema value.

You're, however, ignoring one important thing - setting it just on the database does not mean the model will have the week_starts_on attribute set right after creating, as the insert will only update the primary key attribute on the model.

Only after refreshing/retrieving the model from database will the attribute be set on the model.

E.g.:

$model = Model::query()->create();

Will mean

$model->week_starts_on === null // true

@crynobone
Copy link
Member

Hey there,

We're closing this issue because it's inactive, already solved, old, or not relevant anymore. Feel free to open up a new issue if you're still experiencing this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants