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

Prevent silently discarding attributes feature is not working #44094

Closed
nshiro opened this issue Sep 12, 2022 · 6 comments
Closed

Prevent silently discarding attributes feature is not working #44094

nshiro opened this issue Sep 12, 2022 · 6 comments

Comments

@nshiro
Copy link
Contributor

nshiro commented Sep 12, 2022

  • Laravel Version: 9.29.0
  • PHP Version: 8.0.13

Description:

New feature introduced in Ver.9.28.0 #43893 is not working as expected.

Steps To Reproduce:

Case1

AppServiceProvider

    public function boot()
    {
        Model::preventSilentlyDiscardingAttributes();
    }

User.php

protected $fillable = ['name'];

web.php

Route::get('/', function () {
    $user = new User();
    $user->fill(['hoge' => 'bar']);

    return 'ok';
});

This echos ok.
But this should throw an exception, right?

Case2

My bad. This is not a bug. This is related #33777.

AppServiceProvider

    public function boot()
    {
        Model::preventSilentlyDiscardingAttributes();
    }

User.php

protected $guarded = ['id'];

web.php

Route::get('/', function () {
    $user = new User();
    $user->fill(['hoge' => 'bar']);

    return 'ok';
});

This throws MassAssignmentException.
Add [hoge] to fillable property to allow mass assignment on [App\Models\User].

hoge should be filled, right? If I'm not mistaken.

@driesvints
Copy link
Member

Ping @ralphjsmit

@nshiro
Copy link
Contributor Author

nshiro commented Sep 12, 2022

I think $this->fillableFromArray($attributes) filters the attributes which are not listed in the 'fillable' property which is a one reason that prevents from working correctly.

https://github.com/ralphjsmit/framework/blob/847064365a71d896f73387a022fd99536137155f/src/Illuminate/Database/Eloquent/Model.php#L444

@taylorotwell
Copy link
Member

I have tried to fix it here: eff2275

@ralphjsmit
Copy link
Contributor

Was afk today, will check this tomorrow!

@nshiro
Copy link
Contributor Author

nshiro commented Sep 13, 2022

One thing I noticed.
I didn't know about this feature #33777.
So Case2 described above is not a bug at all.
I was mistaken. 👎

@driesvints
Copy link
Member

This got fixed

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