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

MoneyPHP cast already on Model #59

Open
cblack412 opened this issue Aug 21, 2024 · 4 comments
Open

MoneyPHP cast already on Model #59

cblack412 opened this issue Aug 21, 2024 · 4 comments

Comments

@cblack412
Copy link

Its not really a bug. Your php works perfectly except, I already have cast on my model using MoneyPHP:

    public $casts = [
        'price' => MoneyCast::class,

    ];

So when I add a form field to my panel, I get this error:

Pelmered\FilamentMoneyField\Forms\Components\MoneyInput::Pelmered\FilamentMoneyField\Forms\Components\{closure}(): Return value must be of type ?string, Money\Money returned

This is for my backend panel. I am using my cast for the frontend webshop of which i want to add products via filament panel.
Is there a fix to this?

Thanks!

@pelmered
Copy link
Owner

Yes, that seems like a reasonable use case and something that should be supported.

Would it be possible to share the code of your MoneyCast class to help me implement and test this?

@cblack412
Copy link
Author

cblack412 commented Aug 21, 2024 via email

@cblack412
Copy link
Author

Yes, that seems like a reasonable use case and something that should be supported.

Would it be possible to share the code of your MoneyCast class to help me implement and test this?

<?php

namespace App\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use Money\Currency;
use Money\Money;

class MoneyCast implements CastsAttributes
{
    /**
     * Cast the given value.
     *
     * @param  array<string, mixed>  $attributes
     */
    public function get(Model $model, string $key, mixed $value, array $attributes): Money
    {
        return new Money($value, new Currency('USD'));
    }

    /**
     * Prepare the given value for storage.
     *
     * @param  array<string, mixed>  $attributes
     */
    public function set(Model $model, string $key, mixed $value, array $attributes): mixed
    {
        return $value;
    }
}

@cblack412
Copy link
Author

Here's my composer.json:

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The skeleton application for the Laravel framework.",
    "keywords": ["laravel", "framework"],
    "license": "MIT",
    "require": {
        "php": "^8.3",
        "ext-intl": "*",
        "filament/filament": "^3.2",
        "itsgoingd/clockwork": "^5.2",
        "laravel/cashier": "^15.4",
        "laravel/framework": "^11.9",
        "laravel/jetstream": "^5.1",
        "laravel/sanctum": "^4.0",
        "laravel/tinker": "^2.9",
        "livewire/livewire": "^3.0",
        "moneyphp/money": "^4.5",
        "pelmered/filament-money-field": "^1.4"
    },
    "require-dev": {
        "fakerphp/faker": "^1.23",
        "laravel/pint": "^1.13",
        "laravel/sail": "^1.26",
        "mockery/mockery": "^1.6",
        "nunomaduro/collision": "^8.0",
        "pestphp/pest": "^2.0",
        "pestphp/pest-plugin-laravel": "^2.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi",
            "@php artisan filament:upgrade"
        ],
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi",
            "@php -r \"file_exists('database/database.sqlite') || touch('database/database.sqlite');\"",
            "@php artisan migrate --graceful --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true,
        "allow-plugins": {
            "pestphp/pest-plugin": true,
            "php-http/discovery": true
        }
    },
    "minimum-stability": "stable",
    "prefer-stable": true
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants