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

Model::preventAccessingMissingAttributes() raises exception when extending a model #49656

Closed
sts-ryan-holton opened this issue Jan 11, 2024 · 5 comments

Comments

@sts-ryan-holton
Copy link
Contributor

Laravel Version

10.40.0

PHP Version

8.2.13

Database Driver & Version

MySQL 8

Description

I've recently upgraded to this version of Laravel. I'm using:

 Model::shouldBeStrict(! $this->app->isProduction());

I have a model: App\Models\Reports\Daily\AffiliateDailySummaryStat when extends a template model called App\Models\Reports\AffiliateSummaryStatTemplate. I'm getting the following error:

The attribute [total_redirected] either does not exist or was not retrieved for model [App\Models\Reports\Daily\AffiliateDailySummaryStat].

Now, total_redirected is indeed a column within my database, but I think Laravel is expecting the $casts to be on the AffiliateDailySummaryStat model which isn't great.

Steps To Reproduce

My casts of my template model:

/**
 * The attributes that should be cast.
 *
 * @var array<string, string>
 */
protected $casts = [
    'total_sessions' => 'integer',
    'total_submits' => 'integer',
    'total_leads' => 'integer',
    'total_accepted' => 'integer',
    'total_declined' => 'integer',
    'total_redirected' => 'integer',
    'total_accepted_redirected' => 'integer',
    'total_processing_duration' => 'integer',
    'total_processing_accept_duration' => 'integer',
    'total_processing_decline_duration' => 'integer',
    'avg_processing_duration' => 'integer',
    'avg_processing_accept_duration' => 'integer',
    'avg_processing_decline_duration' => 'integer',
    'logged_at' => 'datetime',
];

And the main model throwing the error:

<?php

namespace App\Models\Reports\Daily;

use App\Models\ModelConstraint;
use App\Enums\ModelConstraintType;
use App\Models\Reports\AffiliateSummaryStatTemplate;
use App\Models\Scopes\ConstrainedByUserViaCompanyScope;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;

class AffiliateDailySummaryStat extends AffiliateSummaryStatTemplate
{
    use HasFactory;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'affiliate_daily_summary_stats';

    /**
     * Get the model's allowed constraints
     */
    public function allowedModelConstraints(): MorphMany
    {
        return $this->morphMany(ModelConstraint::class, 'modelable')
                    ->where('constraint_type', ModelConstraintType::ALLOW->value);
    }

    /**
     * Get the model's blocked constraints
     */
    public function blockedModelConstraints(): MorphMany
    {
        return $this->morphMany(ModelConstraint::class, 'modelable')
                    ->where('constraint_type', ModelConstraintType::BLOCK->value);
    }
}

Does preventAccessingMissingAttributes complain about

@driesvints
Copy link
Member

I think Laravel is expecting the $casts to be on the AffiliateDailySummaryStat model which isn't great.

Correct. You should define this on your extending model as well.

@sts-ryan-holton
Copy link
Contributor Author

Even after defining $casts on both the models the error persists:

The attribute [total_redirected] either does not exist or was not retrieved for model [App\Models\Reports\Daily\AffiliateDailySummaryStat].

@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 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"

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!

@MrPunyapal
Copy link
Contributor

Even after defining $casts on both the models the error persists:

The attribute [total_redirected] either does not exist or was not retrieved for model [App\Models\Reports\Daily\AffiliateDailySummaryStat].

I faced the same issue in the past and found that one of the queries has ->select() without that particular value so please check them again...

@driesvints
Copy link
Member

Feel free to open a new issue once you have a repo which reproduces this.

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