1

In laravel 8 I made a Model app/Models/Role.php

class Role extends Model
{
    use HasFactory;

    public function users(): BelongsToMany
    {
        return $this->belongsToMany(User::class);
    }
}

In routes/web.php I made route

Route::get('/role/{id}/user', function ($id) {
    $users = Role::find($id)->users()->get();
    foreach ($users as $user) {
        echo $user;
    }
});

PhpStorm shows error saying "Method 'find' not found in App\Models\Role". Please help me to fix this. Thanks

Show Screenshot

STA
  • 30,729
  • 8
  • 45
  • 59
  • 1
    There are a lot of tutorials how to configure PHPStorm to read laravel/php stubs and phpdoc. This is too vague for SO. – Jerven Clark Jul 25 '21 at 10:46

2 Answers2

4

For Models in PhpStorm, this should be the definitive answer:

https://stackoverflow.com/a/48847159/1382828

Using @mixin \Eloquent in your models cures all headaches.

CXJ
  • 4,301
  • 3
  • 32
  • 62
2

Its only PhpStorm showing erorr.Ide is not able to recognize methods.If you call that route then get desired output.

To solve

Method 'find' not found in \App\Models\Role

just call query() method

 $users = Role::query()->find($id)->users()->get();

query() returns \Illuminate\Database\Eloquent\Builder

John Lobo
  • 14,355
  • 2
  • 10
  • 20