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

Load unknown "int.php" class in getCastType #49738

Closed
TeoConan opened this issue Jan 18, 2024 · 3 comments
Closed

Load unknown "int.php" class in getCastType #49738

TeoConan opened this issue Jan 18, 2024 · 3 comments

Comments

@TeoConan
Copy link

Laravel Version

10.41

PHP Version

8.3

Database Driver & Version

mysql

Description

Hi everyone

I found a weird bug
I tried to setup a very simple model, with a relation between two tables like this :

class ModelA extends Model
{
    protected $table = 'model_a';
    protected $fillable = ['name', 'product'];

    public function allModelB(): HasMany
    {
        return $this->hasMany(ModelB::class, 'model_b_id', 'id');
    }
}

class ModelB extends Model
{

    protected $table = 'model_b';
    protected $fillable = ['model_b_id', 'prod_file'];

    public function modelA()
    {
        return $this->belongsTo(ModelA::class);
    }
}

So I tried to get some data with :

$modelA = ModelA::where('name', 'mymodela')->first();
$modelB = $modelA->allModelB()->get();

And when I do it, it crash with Warning: require_once(/var/www/html/int.php): Failed to open stream: No such file or directory

I actually found what is wrong in vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php::878

/**
     * Get the type of cast for a model attribute.
     *
     * @param  string  $key
     * @return string
     */
    protected function getCastType($key)
    {
        $castType = $this->getCasts()[$key];

        if (isset(static::$castTypeCache[$castType])) {
            return static::$castTypeCache[$castType];
        }

        if ($this->isCustomDateTimeCast($castType)) {
            $convertedCastType = 'custom_datetime';
        } elseif ($this->isImmutableCustomDateTimeCast($castType)) {
            $convertedCastType = 'immutable_custom_datetime';
        } elseif ($this->isDecimalCast($castType)) {
            $convertedCastType = 'decimal';
        // } elseif (class_exists($castType)) { // Commented this
        //    $convertedCastType = $castType;
        } else {
            $convertedCastType = trim(strtolower($castType));
        }

        return static::$castTypeCache[$castType] = $convertedCastType;
    }

And now it work, because actually it doesn't make sens to find a specific class to cast a int ?
Am I wrong somewhere ?

Complete error log : (I'm not inside a laravel framework but I use illuminate/database package)

Warning: require_once(/var/www/html/int.php): Failed to open stream: No such file or directory in /var/www/html/Lib/autoload.php on line 13

Fatal error: Uncaught Error: Failed opening required '/var/www/html/int.php' (include_path='.:/usr/local/lib/php') in /var/www/html/Lib/autoload.php:13
Stack trace:
  | #0 [internal function]: {closure}('int')
  | #1 /var/www/html/vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php(892): class_exists('int')
  | #2 /var/www/html/vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php(754): Illuminate\Database\Eloquent\Model->getCastType('id')
  | #3 /var/www/html/vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php(2180): Illuminate\Database\Eloquent\Model->castAttribute('id', 321)
  | #4 /var/www/html/vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php(501): Illuminate\Database\Eloquent\Model->transformModelValue('id', 321)
  | #5 /var/www/html/vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php(453): Illuminate\Database\Eloquent\Model->getAttributeValue('id')
  | #6 /var/www/html/vendor/illuminate/database/Eloquent/Relations/Relation.php(285): Illuminate\Database\Eloquent\Model->getAttribute('id')
  | #7 [internal function]: Illuminate\Database\Eloquent\Relations\Relation->Illuminate\Database\Eloquent\Relations\{closure}(Object(Models\Recette), 0)
  | #8 /var/www/html/vendor/illuminate/collections/Arr.php(558): array_map(Object(Closure), Array, Array)
  | #9 /var/www/html/vendor/illuminate/collections/Collection.php(777): Illuminate\Support\Arr::map(Array, Object(Closure))
  | #10 /var/www/html/vendor/illuminate/database/Eloquent/Relations/Relation.php(284): Illuminate\Support\Collection->map(Object(Closure))
  | #11 /var/www/html/vendor/illuminate/database/Eloquent/Relations/HasOneOrMany.php(105): Illuminate\Database\Eloquent\Relations\Relation->getKeys(Array, 'id')
  | #12 /var/www/html/vendor/illuminate/database/Eloquent/Builder.php(776): Illuminate\Database\Eloquent\Relations\HasOneOrMany->addEagerConstraints(Array)
  | #13 /var/www/html/vendor/illuminate/database/Eloquent/Builder.php(754): Illuminate\Database\Eloquent\Builder->eagerLoadRelation(Array, 'emboitages', Object(Closure))
  | #14 /var/www/html/vendor/illuminate/database/Eloquent/Builder.php(722): Illuminate\Database\Eloquent\Builder->eagerLoadRelations(Array)
  | #15 /var/www/html/vendor/illuminate/database/Concerns/BuildsQueries.php(333): Illuminate\Database\Eloquent\Builder->get(Array)
  | #16 /var/www/html/vendor/illuminate/database/Eloquent/Builder.php(447): Illuminate\Database\Eloquent\Builder->first(Array)
  | #17 /var/www/html/Public/index.php(17): Illuminate\Database\Eloquent\Builder->find(321)
  | #18 {main}
 

<br class="Apple-interchange-newline">

Thanks in advance

Steps To Reproduce

Install illuminate/database with composer
Use a relation with a int prop

@crynobone
Copy link
Member

Hey there, thanks for reporting this issue.

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 one separate commit 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"

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!

@timacdonald
Copy link
Member

@TeoConan Have you got any casts on your models?

Can you please look for an 'int' cast? I think you might have one and it should be 'integer' instead.

See: https://laravel.com/docs/10.x/eloquent-mutators#attribute-casting

@crynobone
Copy link
Member

Hey there,

We're closing this issue because it's inactive, already solved, old, or not relevant anymore. Feel free to open up a new issue if you're still experiencing this problem.

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

No branches or pull requests

3 participants