laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.32k stars 10.95k forks source link

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

Closed sharifzadesina closed 2 years ago

sharifzadesina commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

sharifzadesina commented 2 years ago

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 commented 2 years ago

@driesvints any updates on this?

driesvints commented 2 years ago

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