barryvdh/laravel-ide-helper

Exception: No query results for model inside on mutator or accessor

malejandrosr opened this issue · 5 comments

Versions:

  • ide-helper Version: 3.0.0
  • Laravel Version: 10.48.8
  • PHP Version: 8.2.17

Question:

I want to generate the properties and methods of my user model and I have these two access mutators and accessors that I need in my frontend which obtain the user roles and permissions permissions
image

But when running the command php artisan ide-helper:models -W I get the following error:
image

Any solution to this problem?

mfn commented

I can't make out anything in the screenshots, please paste the raw logs (use markdown "tripple-backticks" formatting please)

Of course, this is the log that returns:

Exception: No query results for model [App\Models\Role].
Could not analyze class App\Models\User.

Trace:
#0 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/app/Models/User.php(146): Illuminate\Database\Eloquent\Relations\BelongsToMany->firstOrFail()
#1 [internal function]: App\Models\User->abilityRolePermissions()
#2 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/vendor/barryvdh/laravel-ide-helper/src/Console/ModelsCommand.php(1148): ReflectionMethod->invoke(Object(App\Models\User))
#3 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/vendor/barryvdh/laravel-ide-helper/src/Console/ModelsCommand.php(595): Barryvdh\LaravelIdeHelper\Console\ModelsCommand->getAttributeTypes(Object(App\Models\User), Object(ReflectionMethod))
#4 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/vendor/barryvdh/laravel-ide-helper/src/Console/ModelsCommand.php(289): Barryvdh\LaravelIdeHelper\Console\ModelsCommand->getPropertiesFromMethods(Object(App\Models\User))
#5 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/vendor/barryvdh/laravel-ide-helper/src/Console/ModelsCommand.php(176): Barryvdh\LaravelIdeHelper\Console\ModelsCommand->generateDocs(Array, Array)
#6 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Barryvdh\LaravelIdeHelper\Console\ModelsCommand->handle()
#7 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#8 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#9 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#10 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/vendor/laravel/framework/src/Illuminate/Container/Container.php(662): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#11 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/vendor/laravel/framework/src/Illuminate/Console/Command.php(211): Illuminate\Container\Container->call(Array)
#12 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/vendor/symfony/console/Command/Command.php(326): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#13 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/vendor/laravel/framework/src/Illuminate/Console/Command.php(180): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#14 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/vendor/symfony/console/Application.php(1096): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#15 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/vendor/symfony/console/Application.php(324): Symfony\Component\Console\Application->doRunCommand(Object(Barryvdh\LaravelIdeHelper\Console\ModelsCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#16 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/vendor/symfony/console/Application.php(175): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#17 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(201): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#18 /Users/alejandrosalgado/Documents/Web/Sites/vue-app/artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#19 {main}
mfn commented

What is abilityRolePermissions()? Is User.php(146) part of that method?

I've a feeling this is what causes the problem: in \Barryvdh\LaravelIdeHelper\Console\ModelsCommand::getAttributeTypes we're calling this code:

        // Private/protected ReflectionMethods require setAccessible prior to PHP 8.1
        $reflectionMethod->setAccessible(true);

        /** @var Attribute $attribute */
        $attribute = $reflectionMethod->invoke($model);

Yes, it's a mutator/accessor where I obtain the permissions of the user role, for that there I call the role relationship that my User model has, after that I call the permissions() relationship that my Role model has.

mfn commented

I can't fully make sense of your problem. Your best change is to reduce it to a minimal reproducible example and put it up in a repo so me/someone can check it out.