beyondcode / laravel-er-diagram-generator

Generate entity relation diagrams from your Laravel model files.
MIT License
1.92k stars 191 forks source link

Laravel 5.5, calling non-existent getPivotClass on BelongsToMany #82

Closed magnusblb closed 7 months ago

magnusblb commented 4 years ago

Hi all,

I am in doubt whether this classifies as an "issue" but for the purpose of documentation and helping others who might run into the same problem, I thought I'd type it down.

For a project still using Laravel 5.5 (yes haven't been able to upgrade and needed to generate a diagram), I installed this package and ran it. I got the exception:

Call to undefined method Illuminate\Database\Query\Builder::getPivotClass()

after the models have been inspected.

The ER diagram generator calls a method by that name on the Eloquent Relationship, but since that method does not exist in Laravel 5.5 it tries to call a method with the same name on the Query\Builder class, triggering the exception.

Workaround:

I snatched the getPivotClass() implementation from Laravel 5.8 (it's just a few lines of code, included below) and pasted it into the Eloquent\BelongsToMany relationship and that seems to have done the trick.

public function getPivotClass() { return $this->using ?? Pivot::class; }

JacksonBates commented 4 years ago

Hmm, I'm not comfortable monkey-patching my Laravel install to achieve this.

I wonder if there is another workaround at the individual model level within my app's codebase that can be applied, or whether there is a suitable fix to this package that would allow out of the box success on Laravel 5.5?

It's a bit beyond me at the moment, but I'll give it some thought.

54853315 commented 3 years ago

我发现 getPivotClass 的调用是从v1.4版本开始出现的: https://github.com/beyondcode/laravel-er-diagram-generator/blob/1.4.0/src/GraphBuilder.php

Laravel5.5及以下版本的使用者,应使用v1.3版本则没有此问题。

建议作者能够在composer.json中声明所对laravel框架版本的限制,会减少一些类似问题的排查。