Back

while trying phpstan with laravel to learn strict rules for the model

class Feature extends Model
{
protected $guarded = [];

/**
* @return BelongsToMany<Plan, Feature>
*/
public function plans(): BelongsToMany
{
return $this->belongsToMany(Plan::class);
}
}

on level 6
 Method App\Models\Feature::plans() should return                    
Illuminate\Database\Eloquent\Relations\BelongsToMany<App\Models\Plan,
App\Models\Feature> but returns
Illuminate\Database\Eloquent\Relations\BelongsToMany<App\Models\Plan,
$this(App\Models\Feature)>.
šŸ’” Template type TDeclaringModel on class
Illuminate\Database\Eloquent\Relations\BelongsToMany is not
covariant.

how to solve this
Note: I dont wanna use larastan , i want to focus php

1 ā€¢ ā€¢

202

ā€¢

0 / 1000

try replacing this:

/**
* @return BelongsToMany<Plan, Feature>
*/

with
/**
* @return BelongsToMany<Plan, $this>
*/

1 ā€¢ ā€¢

114

ā€¢

@sharoon thank you so much it worked, but as i checked the docs i did not find this. Where did you get this ?

1 ā€¢ ā€¢

66

ā€¢

Masum, Iā€™m glad it worked. Actually I was looking into the source code of Pinkary few days ago, and noticed that update there and it was in my mind and when I saw your error, I just guessed the answer to you.

1 ā€¢ ā€¢

118

ā€¢

@sharoon its actually pretty good for learning by looking at the code of pinkary

ā€¢ ā€¢

106

ā€¢