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

fill method not working on mutators if you have some guarded attributes #51065

Closed
mokhosh opened this issue Apr 15, 2024 · 2 comments
Closed

Comments

@mokhosh
Copy link
Contributor

mokhosh commented Apr 15, 2024

Laravel Version

11.1.0

PHP Version

8.3.3

Database Driver & Version

No response

Description

If you have a user model like this:

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Foundation\Auth\User as IlluminateUser;

class User extends IlluminateUser
{
    protected $guarded = [
        'remember_token',
    ];

    protected $appends = [
        'location',
    ];

    public function location(): Attribute
    {
        return Attribute::make(
            get: fn (mixed $value, array $attributes) => [
                'lat' => $attributes['latitude'],
                'lng' => $attributes['longitude'],
            ],
            set: fn (array $value) => [
                'latitude' => $value['lat'],
                'longitude' => $value['lng'],
            ],
        );
    }
}

This doesn't work and completely ignores location:

$user->fill(['location' => [
    'lat' => 1,
    'lng' => 2,
]])

This works:

$user->forceFill(['location' => [
    'lat' => 1,
    'lng' => 2,
]])

Interestingly, fill works fine with this:

protected $guarded = [];

but not if you have guarded the remember_token as per the original example!

This also works, but you have to add all attributes again, and you can't mix and match with guarded:

protected $fillable = [
    'location',
];

Steps To Reproduce

Add latitude and longitude columns to the users table in a fresh Laravel installation, and paste the codes above, and tinker with the model to see the behaviour.

@driesvints
Copy link
Member

Heya, thanks for reporting.

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"

Please 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!

@driesvints
Copy link
Member

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

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

2 participants