pelmered / filament-money-field

Money field powered by Money PHP
MIT License
52 stars 20 forks source link

MoneyPHP cast already on Model #59

Open cblack412 opened 3 weeks ago

cblack412 commented 3 weeks ago

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 commented 3 weeks ago

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 commented 3 weeks ago

Yes, certainly! When I get home from work I will post the code. Thanks for responding!

Sent from the all new AOL app for iOS

On Wednesday, August 21, 2024, 10:02 AM, Peter Elmered @.***> wrote:

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?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

cblack412 commented 3 weeks ago

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 commented 3 weeks ago

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
}