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

Fill the Currrency Field without checking #128

Merged
merged 4 commits into from
Nov 9, 2022
Merged

Fill the Currrency Field without checking #128

merged 4 commits into from
Nov 9, 2022

Conversation

A-Ghorab
Copy link
Contributor

@A-Ghorab A-Ghorab commented Sep 24, 2022

Dears,

During my development i found that the currency fields doesn't get filled with new instances and i have to fill the currency manually

i checked the code and it seems that it checks if the field exists in the attribute before filling which for new instances wouldn't be correct.

so i don't think we need to do the check as it's already passed as a parameter.

For anyone facing the same issue you can just initialize the currency field using attributes inside the model

class Request extends Model
{
    protected $attributes = [
        'currency' => '',
    ];
}

the currency field might not exist (initialized) yet, so it doesn't fill the currency field when a new instance is created
@ricardogobbosouza
Copy link
Member

Hi @A-Ghorab
You can set the coin to be casted in a field or just force a coin type.

Ex:

use Cknow\Money\Casts\MoneyIntegerCast;
use Cknow\Money\Casts\MoneyStringCast;

protected $casts = [
    // cast money as integer using the defined currency
    'money' => MoneyIntegerCast::class . ':AUD',

    // cast money as string using the currency defined in the model attribute 'currency'
    'money' => MoneyStringCast::class . ':currency',
];

See: https://github.com/cknow/laravel-money#casts

@A-Ghorab
Copy link
Contributor Author

A-Ghorab commented Nov 2, 2022

Hi @A-Ghorab You can set the coin to be casted in a field or just force a coin type.

Ex:

use Cknow\Money\Casts\MoneyIntegerCast;
use Cknow\Money\Casts\MoneyStringCast;

protected $casts = [
    // cast money as integer using the defined currency
    'money' => MoneyIntegerCast::class . ':AUD',

    // cast money as string using the currency defined in the model attribute 'currency'
    'money' => MoneyStringCast::class . ':currency',
];

See: https://github.com/cknow/laravel-money#casts

i'm using this method

> protected $casts = [
>     // cast money as string using the currency defined in the model attribute 'currency'
>     'money' => MoneyStringCast::class . ':currency',
> ];

but to clarify the issue this is an example of the code

$model = new Model();
$model->money = 10; 
$model->currency; //null

this is duo to the check if attribute exists or not so we to make it work i have to fill currency;

$model = new Model();
$model->currency = ''; // just a simple fill with any value
$model->money = 10; 
$model->currency; //USD

AGhorabHekouky and others added 2 commits November 6, 2022 21:47
add check if the field is a currency or an attribute
@A-Ghorab
Copy link
Contributor Author

A-Ghorab commented Nov 6, 2022

Ok running the tests i found that it might break some cases, so added a currency type check to the method so no more failing tests.

Also added one more test to verify if currency field is getting filled automatically or not.

@ricardogobbosouza
Copy link
Member

Thanks @A-Ghorab

@ricardogobbosouza ricardogobbosouza merged commit 8c3ed1c into cknow:main Nov 9, 2022
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

Successfully merging this pull request may close these issues.

None yet

3 participants