laravel-shift / factory-generator

MIT License
41 stars 20 forks source link

BadMethodCallException when using laravel-promocodes package #13

Closed anhofmann closed 2 years ago

anhofmann commented 3 years ago

I was running php artisan generate:factory and it returned this exception:


   BadMethodCallException 

  Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::getForeignKey()

  at vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:50
     46▕      * @throws \BadMethodCallException
     47▕      */
     48▕     protected static function throwBadMethodCallException($method)
     49▕     {
  ➜  50▕         throw new BadMethodCallException(sprintf(
     51▕             'Call to undefined method %s::%s()', static::class, $method
     52▕         ));
     53▕     }
     54▕ }

  • Bad Method Call: Did you mean Illuminate\Database\Eloquent\Relations\BelongsToMany::getForeignPivotKeyName() ? 

      +23 vendor frames 
  24  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

After some debugging I found out that this happens in my user model. Further investigations revealed that the trait Rewardable was the problem. This trait comes from this package: https://github.com/anhofmann/laravel-promocodes I forked this package, but I think it will also happen in the original package: https://github.com/zgabievi/laravel-promocodes
I know that laravel-promocodes is pretty old, but it's working well in my laravel 8 app. Do you see a way to fix this? At this point I don't know if this is a bug in factory-generator or laravel-promocodes.

RckMrkr commented 2 years ago

I get a the same error just using Laravel Jetstream and using teams. Doing that includes a trait called HasTeams where there is a belongsToMany relation, which breaks everything :(

I had hoped that Laravel Jetstream was being used enough to not run into issues like these... from a year ago.

Naoray commented 2 years ago

You are welcome to make a PR to fix this error. -- Krishan König

Website https://bitmate.dev/ | Github https://github.com/naoray | Twitter https://twitter.com/NaorayKroaths

jasonmccreary commented 2 years ago

I've been meaning to revisit this package. It's on the list. Unfortunately, it's not very high up. I'll try to do a live-stream and focus on the open issues soon.

tshafer commented 2 years ago

Add

   if ($relationObj instanceof BelongsToMany) {
        return $relationObj->getForeignPivotKeyName();
   }

to Naoray\EloquentModelAnalyzer\InteractsWithRelationMethods on line 104.

This worked for me

Naoray commented 2 years ago

@tshafer @anhofmann Sorry for the delay. This should now be fixed in the latest release of eloquent-model-analyzer.

Naoray commented 2 years ago

@jasonmccreary can you please update composer on this package to utilize the change on the underlying package?

jasonmccreary commented 2 years ago

@Naoray, sure, what's the new constraint?

Naoray commented 2 years ago

v3.0.1

On Mon 22. Aug 2022 at 15:10, Jason McCreary @.***> wrote:

@Naoray https://github.com/Naoray, sure, what's the new constraint?

— Reply to this email directly, view it on GitHub https://github.com/laravel-shift/factory-generator/issues/13#issuecomment-1222338876, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACNPA5D7VMIJNQFQBTYX4ZDV2N33HANCNFSM436ILU2Q . You are receiving this because you were mentioned.Message ID: @.***>

-- Krishan König

Website https://bitmate.dev/ | Github https://github.com/naoray | Twitter https://twitter.com/NaorayKroaths

jasonmccreary commented 2 years ago

This has been incremented on master. There are no tags for this package.