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

Unable to cast database attributes to objects using custom casts #43870

Closed
sharifzadesina opened this issue Aug 26, 2022 · 6 comments · Fixed by #43959
Closed

Unable to cast database attributes to objects using custom casts #43870

sharifzadesina opened this issue Aug 26, 2022 · 6 comments · Fixed by #43959
Labels

Comments

@sharifzadesina
Copy link
Contributor

sharifzadesina commented Aug 26, 2022

  • Laravel Version: 9.26.1
  • PHP Version: 8.1
  • Database Driver & Version: Doesn't matter

Description:

It is not possible to cast database attributes to objects using custom cast classes and then convert them to an array.

Steps To Reproduce:

Models class:

<?php

namespace App\Models;

use App\Casts\GMPCast;
use Illuminate\Database\Eloquent\Model;

class Balance extends Model
{
    protected $casts = [
        'amount' => GMPCast::class,
    ];
}

and our custom cast class:

<?php

namespace App\Casts;

use GMP;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Contracts\Database\Eloquent\SerializesCastableAttributes;

class GMPCast implements CastsAttributes, SerializesCastableAttributes
{
    public function get($model, $key, $value, $attributes)
    {
        return gmp_init($value, 10);
    }

    public function set($model, $key, $value, $attributes)
    {
        return gmp_strval($value, 10);
    }

    public function serialize($model, string $key, $value, array $attributes)
    {
        return gmp_strval($value, 10);
    }
}

The code:

Balance::all()->toArray();

The error that happens:

Object of class GMP could not be converted to bool

The reason is obvious, here: HasAttributes.php#L285
we first cast the amount attribute to GMP class using custom cast, then inside the next if statements (HasAttributes.php#L292, HasAttributes.php#L296, HasAttributes.php#L306 and HasAttributes.php#L933) we try to convert it to boolean, so the error happens.
The solution is to try to check the $attributes[$key] by not converting it to boolean implicitly.

@driesvints
Copy link
Member

Heya, thanks for reporting.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as separate commits on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Please do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@sharifzadesina
Copy link
Contributor Author

sharifzadesina commented Aug 26, 2022

Hello,
This was a simple bug, I don't know why I had to create a new repo just for it.
Anyways, here you go: https://github.com/sharifzadesina/bug-report

Please NOTE: you need to connect to a working DB, run the migrations, and add a balance record to the database, then if you visit the welcome page you will see the error. (GMP extension is required because I am using it for custom cast)

Link to the Bug Commit

@driesvints
Copy link
Member

Please add the code to create a balance record in order to reproduce the bug.

@sharifzadesina
Copy link
Contributor Author

sharifzadesina commented Aug 26, 2022

Hello,
Added DB config and seed to create the record in the database. so you can reproduce the error by running php artisan migrate --seed and then visiting welcome page.

@sharifzadesina
Copy link
Contributor Author

@driesvints any updates on this?

@driesvints
Copy link
Member

Thanks, I managed to send in a fix for this.

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

Successfully merging a pull request may close this issue.

2 participants