In laravel 11 with larastan/larastan 2.9 app I with request :
$managers = ModelHasPermission
::getByPermissionId(PERMISSION_APP_MANAGER)
->with('user') // ERROR POINTING THIS LINE 37
->get();
foreach ($managers as $manager) {
$manager->user->notify(new ContactUsSentNotification(contactUs: $contactUs, manager: $manager->user));
}
Running larastan with level = 6
I got error :
37 Relation 'user' is not found in App\Models\ModelHasPermission model.
43 Access to an undefined property App\Models\ModelHasPermission::$user.
š” Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property
43 Access to an undefined property App\Models\ModelHasPermission::$user.
š” Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined
But In model app/Models/ModelHasPermission.php I have :
<?php
namespace App\Models;
use DB;
use Illuminate\Database\Eloquent\Model;
class ModelHasPermission extends Model
{
protected $table = 'model_has_permissions';
protected $primaryKey = 'id';
...
public function user()
{
return $this->belongsTo(User::class, 'model_id');
}
}
How have I to define user method not to have this/and next on line 43 Access to an undefined property
errors ?
Thanks to kris, this solved the problem
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'model_id');
}