filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
18.07k stars 2.83k forks source link

Setting select to nullable not working on save ^3.0-stable (v3.0.19) but working on older version 3.0.14 #7715

Closed khanakia closed 1 year ago

khanakia commented 1 year ago

Package

filament/filament

Package Version

^3.0-stable (v3.0.19)

Laravel Version

10.18.0

Livewire Version

v3

PHP Version

PHP 8.2

Problem description

Check the Dsicussion https://github.com/filamentphp/filament/discussions/7712

Hello,

this is what i have as you can see state_id is nullable and when i clear the select field and saving does not works Note these points i have checked already

  1. I have all the fields are unguarded already
  2. The table column state_id type is nullable
  3. I debug the laravel query log it sends this only like i updated the rate and unselected the state_id update "tax_rates" set "rate" = ?, "updated_at" = ? where "id" = ? ["35.001","2023-08-13 08:13:44",2]

Note: I am using Postgres as my database

return $form
            ->schema([
                Forms\Components\Select::make('country_id')
                    ->searchable()
                    ->preload()
                    ->live()
                    ->relationship('country', 'name')
                    ->nullable(),
                Forms\Components\Select::make('state_id')
                    ->searchable()
                    ->preload()
                    ->relationship('state', 'name')
                    ->options(fn (Get $get): Collection => State::query()
                    ->where('country_id', $get('country_id'))
                    ->pluck('name', 'id'))
                    ->nullable(),
                Forms\Components\Select::make('tax_class_id')
                    ->relationship('tax_class', 'name')
                    ->required(),
                Forms\Components\Select::make('tax_rate_code_id')
                    ->relationship('tax_rate_code', 'name')
                    ->required(),
                Forms\Components\TextInput::make('rate')
                    ->required()
                    ->numeric(),
                Forms\Components\Toggle::make('status'),
            ]);

Expected behavior

It should send stated_id=null also in the update query

Steps to reproduce

I have created a demo reproducible repo also here https://github.com/khanakia/filament_selectnull

It contains very minimal code just to reproduce the issue in MYSQL also.

How to run

  1. I have committed the .env and filament_selectnulldb.sql files within the root directory
  2. Restore the mysql database file
  3. Run php artisan serve
  4. Login with user: admin@admin.com and pass: admin
  5. Edit any Item and unselect the brand and save

Here's the attached video:

https://github.com/filamentphp/filament/assets/2174170/6b1a45b0-bba4-4443-bcf6-a5b7dbcc9f28

Reproduction repository

https://github.com/khanakia/filament_selectnull

Relevant log output

This bug only exists in the newer version ^3.0-stable (v3.0.19)

I was testing the filament demo main repo and I just downgraded to the same version as demo repo v3.0.14 to my repo and it worked.

danharrin commented 1 year ago

Can you please pinpoint exactly which release between .14 and .19 caused it?

khanakia commented 1 year ago

I have mentioned it already ^3.0-stable (v3.0.19) :hushed:

Screenshot 2023-08-14 at 10 44 07 AM

danharrin commented 1 year ago

You mentioned that it worked when you downgraded to .14, did you try .18? I don't see any changes to the select component that were made in .19

khanakia commented 1 year ago

I downgraded to .14 because i saw it was working on the filament demo repo https://github.com/filamentphp/demo/tree/main/app/Filament which uses the .14

So i wanted to make sure that bug exists in the latest version only.

Also i shared a minimal repo to reproduce the issue here: https://github.com/khanakia/filament_selectnull

khanakia commented 1 year ago

PS: Also checked on "filament/filament": "v3.0.18" same issue persist

I just checked

khanakia commented 1 year ago

I just digged further this commit created the issue

https://github.com/filamentphp/filament/commit/6dcb86ce81cafb82f2c82fd06cd4f802143e64fc?diff=unified

danharrin commented 1 year ago

Found the issue. Your column isn't $fillable.

khanakia commented 1 year ago

I see I thought Model::unguard(); will simply make all the fields fillable.

Can we add this somewhere in docs like to define $fillable for select when it is nullable so it will save time for anyone having the same situation 😄

danharrin commented 1 year ago

Model::unguard(); should also work