Larastan: Call to an undefined method Illuminate\Database\Eloquent\Model::myHasManyRelation().

Created on 25 Apr 2019  路  14Comments  路  Source: nunomaduro/larastan

  • Larastan Version: 0.3.16
  • --level used: max

Description

false positive?

when trying to reach myHasManyRelationship as property it does find id. However when fetching the relationship as query it is found as error. Call to an undefined method Illuminate\Database\Eloquent\Model::myHasManyRelation()
notable is the fact that is takes the eloquent model class, and not the mainModel class itsself.

I love your package. all love to your work 鉂わ笍

Laravel code where the issue was found

// controller where querying model

$mainModel         = MainModel::whereType('type')
                ->whereExternalId($externalId)
                ->firstOrFail();
            $lastDataPoint = $mainModel
                ->myHasManyRelationship()
                ->orderByDesc('log_at')
                ->firstOrFail();


// model relationship
   /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function myHasManyRelationship()
    {
        return $this->hasMany(Mt::class);
    }
false positive

All 14 comments

Thank you.

Yes, Eloquent is not our 100% friend yet.

I will continiou to shoot issues as I think it will help in finding where the issues come from, and will give a backlog for it. Will have a holiday period in a few weeks, I hope I could help resolve the issue.

same thing happen to me but more like Access to an undefined property object:

@hengkydev Without a code sample we cannot help you.

@ondrejmirtes sorry because it same reproduce as him

this my code

ContactModel

/**
     * relation contact has one study program
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function study_program()
    {
        return $this->hasOne('App\Models\Campus\StudyProgram','sfid','intended_study_program__c');
    }

Controller

Auth::user()->contact->study_program->id

just difference call, i call as object and @adminfriso call as method
if i change to method the result is same
Call to an undefined method object::study_program().

So the problem is that Auth::user()->contact is of object type and not whatever entity it actually is.

@ondrejmirtes
no is entity
in UserModel

/**
     * relation user has one to contact
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function contact()
    {
        return $this->hasOne('App\Models\Auth\Contact','heroku_id__c','contact_heroku_id');
    }

and my code is working properly just when running
php artisan code:analyse
is showing that message
Call to an undefined method object::study_program().

Yes, Larastan does not know that contact() returns App\Models\Auth\Contact.

i try another reproduce, and i guest the problem
is similarity of method name i don't know where it is could be same as table name or etc
( even only that method is naming as contact() in that class )
when i try rename contact() to kontak()
larastan didn't recognize it as error
and the result is fine

i used this , and is fine larastan didn't recognize it as error

Auth::user()->contact->{'study_program'}->id

I'm trying to find own problems with Eloquent+Larastan, and I think is caused by external libraries or code. Why I think this?

I build a basic project: Laravel+Larastan+Models.

Laravel 5.8 with artisan [OK]

The code pass code:analyse: https://github.com/pablorsk/larastan-example/commit/9ffac2933c67e467c54666e66ea94c52fb3066ca#diff-69487ee344e81a61775c6ae6fd03f95cR21 (testBasicQueryWithProblems pass too).

Also, on tests, I replicate this issue, but I dont have problems with analyse. Then, we need to find the problem with big projects. Also, we need to detect why lines like Book::where('id', '>', 1)->chapters->firstOrFailX(); dont report any phpstan error.

Command ran: php artisan code:analyse -l 7 -p "app,tests"

Laravel 5.8 calling phpstan directly [OK]

For example ./vendor/bin/phpstan analyse -c resources/rules/phpstan.src.neon: https://github.com/pablorsk/larastan-example/pull/2/files#diff-04c6e90faac2675aa89e2176d2eec7d8R26

Is it possible to fix this for the new upcoming major version?

Can you test this with the latest changes in master branch?

with the your last changes in master it is fixed

Was this page helpful?
0 / 5 - 0 ratings