laravel / framework

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

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

Open mauricekindermann opened 4 days ago

mauricekindermann commented 4 days ago

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 commented 4 days ago

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 commented 3 days ago

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 commented 3 days ago

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!