diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index ca2bfb891..0b5085b8f 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -230,7 +230,9 @@ protected function generateDocs($loadModels, $ignore = '') $ignore[] = $name; $this->nullableColumns = []; } catch (\Throwable $e) { - $this->error("Exception: " . $e->getMessage() . "\nCould not analyze class $name.\n\nTrace:\n".$e->getTraceAsString()); + $this->error("Exception: " . $e->getMessage() . + "\nCould not analyze class $name.\n\nTrace:\n" . + $e->getTraceAsString()); } } } @@ -476,7 +478,7 @@ protected function getPropertiesFromMethods($model) // php 7.x type or fallback to docblock $type = (string)$this->getReturnTypeFromDocBlock($reflection); } - + $file = new \SplFileObject($reflection->getFileName()); $file->seek($reflection->getStartLine() - 1); @@ -510,7 +512,12 @@ protected function getPropertiesFromMethods($model) continue; } - $relationObj = $model->$method(); + // Adding constraints requires reading model properties which + // can cause errors. Since we don't need constraints we can + // disable them when we fetch the relation to avoid errors. + $relationObj = Relation::noConstraints(function () use ($model, $method) { + return $model->$method(); + }); if ($relationObj instanceof Relation) { $relatedModel = '\\' . get_class($relationObj->getRelated()); diff --git a/tests/Console/ModelsCommand/Relations/Models/Simple.php b/tests/Console/ModelsCommand/Relations/Models/Simple.php index 56e46a96c..84f5c87e1 100644 --- a/tests/Console/ModelsCommand/Relations/Models/Simple.php +++ b/tests/Console/ModelsCommand/Relations/Models/Simple.php @@ -62,4 +62,9 @@ public function relationBelongsToInAnotherNamespace(): BelongsTo { return $this->belongsTo(AnotherModel::class); } + + public function relationBelongsToSameNameAsColumn(): BelongsTo + { + return $this->belongsTo(AnotherModel::class, __FUNCTION__); + } } diff --git a/tests/Console/ModelsCommand/Relations/Test.php b/tests/Console/ModelsCommand/Relations/Test.php index 03e078f93..e8345d458 100644 --- a/tests/Console/ModelsCommand/Relations/Test.php +++ b/tests/Console/ModelsCommand/Relations/Test.php @@ -73,6 +73,7 @@ public function test(): void * @property-read \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\ModelsOtherNamespace\AnotherModel $relationBelongsToInAnotherNamespace * @property-read \Illuminate\Database\Eloquent\Collection|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple[] $relationBelongsToMany * @property-read int|null $relation_belongs_to_many_count + * @property-read \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\ModelsOtherNamespace\AnotherModel $relationBelongsToSameNameAsColumn * @property-read \Illuminate\Database\Eloquent\Collection|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple[] $relationHasMany * @property-read int|null $relation_has_many_count * @property-read \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple $relationHasOne @@ -136,6 +137,11 @@ public function relationBelongsToInAnotherNamespace(): BelongsTo { return $this->belongsTo(AnotherModel::class); } + + public function relationBelongsToSameNameAsColumn(): BelongsTo + { + return $this->belongsTo(AnotherModel::class, __FUNCTION__); + } } PHP;