-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Fix cast issue #2651
Comments
To merge #2257 PR, I think we might need to override some methods used in castAttribute method and writing tests to cover every data type declared in |
@hans-thomas Or, if the |
The |
@hans-thomas I did some reading. Learn something new everyday. Some info states casts are used used in saving/updating. Some info states the opposite. I know this: When testing an eloquent model, This changes my mind on this issue. If Laravel doesn't cast when saving/updating to the database, neither should this package. It should be left up to the user to use Attribute (which I originally did to solve this issue). Since I opened this issue, I'm now closing it. If someone wants this functionality changed, they can open a new issue. |
@rbruhn Laravel doesn't support schemaless databases but the purpose of this package is to add a schemaless database (MongoDB) support to Laravel. So, that's why we need to override some methods or add new methods. I guess this casting is done in
I don't disagree with you, but this problem doesn't have to exist to solve. |
I recently hit to this problem. Something to do also with upgrading to Laravel 10 I believe. They dropped the separate $dates property and date casting is now done in $casts property. I have of course made the needed changes on my codebase, but still issues. For some reason in some occasions date casting doesn't seem to work with mongo. Here is example of the change I needed to do because of this (it used to work before update): @hans-thomas are you working for a PR for casting? All these issues and PRs seems to be closed, although there clearly is some problems. Strange thing is that in some parts of my codebase date casting seems to be working normally. |
I am experiencing this same issue. I am trying to mutate/cast the attribute when storing in the database. Although I'd rather use some of the built-in casts, I did find that creating a custom cast did solve the use-case for me. This didn't work for me (just like the OP stated: protected $casts = [
'age' => 'int',
]; However, this DID work, making a custom reusable integer cast: // MyModel.php
protected $casts = [
'age' => IntegerCast::class,
];
// IntegerCast.php
<?php
namespace App\Casts;
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
class IntegerCast implements CastsInboundAttributes
{
public function set($model, string $key, mixed $value, array $attributes): int
{
return (int) $value;
}
} Or, for a one-off cast, this worked as well for me: protected function age(): Attribute
{
return Attribute::make(set: static fn (string $age): int => (int) $age);
} |
@apeisa I will check this issue ASAP but if you find a use case that breaks date casting, it can help a lot to fix this issue faster.
Can you check that when it's working fine? On creating/updating or retrieving? I guess it's working correctly on retrieving. |
@coreycoburn class casting is done by default in |
As reported in #2257 , #2199 , #1580 there is an issue with casting and saving to the database. @alcaeus asked for a test example in a new issue. A sample test was provided by @hans-thomas but I figured I would add one here that's more inline with current tests.
The pull request from #2257 works.
First, change User model to make age and integer. This should not effect any other tests as an integer is used in those tests.
In ModelTest, add the following test. Here we set age as string. It will fail because the cast type is not being saved to the database.
The text was updated successfully, but these errors were encountered: