Comments (9)
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);
}
from laravel-mongodb.
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 castAttribute
method.
from laravel-mongodb.
@hans-thomas Or, if the parent::setAttribute
is not casting (which it's not) perhaps it's the wrong method to extend.
from laravel-mongodb.
The setAttribute
method casts attributes on retrieving from DB. Because of that, attributes were not cast in creating or updating actions. That's why we need this PR #2257. However, as I said, we need to write tests to cover all supported data types.
from laravel-mongodb.
@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, castAttribute()
is never called when saving or updating.
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.
from laravel-mongodb.
@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 PDO
or database layer in SQL-based databases because we determined the column data type before any saving/updating actions.
It should be left up to the user to use Attribute (which I originally did to solve this issue).
I don't disagree with you, but this problem doesn't have to exist to solve.
from laravel-mongodb.
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.
from laravel-mongodb.
@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.
Strange thing is that in some parts of my codebase date casting seems to be working normally.
Can you check that when it's working fine? On creating/updating or retrieving? I guess it's working correctly on retrieving.
from laravel-mongodb.
@coreycoburn class casting is done by default in setAttribute
. The problem is casting an attribute to a native type.
from laravel-mongodb.
Related Issues (20)
- Custom datetime casting works, but not if you define custom format
- Call to undefined method compileWhereSub() HOT 4
- Support for morphTo relations broken in 4.1 HOT 4
- model id property returns the builder and not the id
- 🍃 Meet the MongoDB PHP Team HOT 1
- BelongsToMany sync does't use configured foreign key
- Did you mean Jenssegers\Mongodb\Query\Builder::compileWhereRaw() ? HOT 2
- id or _id not populated after calling create function #1820 HOT 8
- ObjectId in query builder not working HOT 6
- function addUpdatedAtColumn use array_merge bug HOT 1
- Cannot query with an array position HOT 2
- Add documentation on polymorphic relationships
- Fetching data via tinker HOT 4
- [Feature Request] Queryable Encryption or CSFLE HOT 1
- Call to undefined method MongoDB\Laravel\Query\Builder::compileWhereSub() HOT 2
- Is MongoDB support Laravel Octane (FrankenPHP server) HOT 1
- whereHas issue HOT 1
- Fatal casting issues HOT 1
- Saving/refreshing model doesn't reset `Model::$unset` HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-mongodb.