Giter Club home page Giter Club logo

Comments (9)

coreycoburn avatar coreycoburn commented on May 28, 2024 1

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.

hans-thomas avatar hans-thomas commented on May 28, 2024

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.

rbruhn avatar rbruhn commented on May 28, 2024

@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.

hans-thomas avatar hans-thomas commented on May 28, 2024

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.

rbruhn avatar rbruhn commented on May 28, 2024

@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.

hans-thomas avatar hans-thomas commented on May 28, 2024

@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.

apeisa avatar apeisa commented on May 28, 2024

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):
image

@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.

hans-thomas avatar hans-thomas commented on May 28, 2024

@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.

hans-thomas avatar hans-thomas commented on May 28, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.