Skip to content

Commit 76993b8

Browse files
authored
Merge pull request #3826 from Laravel-Backpack/one-more-check-in-column-auto-infer
If possible relation method requires parameters, it's not a relation method.
2 parents 54525d5 + 05cd190 commit 76993b8

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/app/Library/CrudPanel/CrudPanel.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,4 +488,22 @@ private function getRelatedEntries($model, $relationString)
488488

489489
return $results;
490490
}
491+
492+
/**
493+
* Check if the method in the given model has any parameters.
494+
*
495+
* @param object $model
496+
* @param string $method
497+
* @return bool
498+
*/
499+
private function modelMethodHasParameters($model, $method)
500+
{
501+
$reflectClassMethod = new \ReflectionMethod(get_class($model), $method);
502+
503+
if ($reflectClassMethod->getNumberOfParameters() > 0) {
504+
return true;
505+
}
506+
507+
return false;
508+
}
491509
}

src/app/Library/CrudPanel/Traits/ColumnsProtectedMethods.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,19 @@ protected function makeSureColumnHasEntity($column)
146146
// if the first part of the string exists as method,
147147
// it is a relationship
148148
if (method_exists($this->model, $possibleMethodName)) {
149-
$column['entity'] = $column['name'];
149+
150+
// if it has parameters it's not a relation method.
151+
$column['entity'] = $this->modelMethodHasParameters($this->model, $possibleMethodName) ? false : $column['name'];
150152

151153
return $column;
152154
}
153155
}
154156

155157
// if there's a method on the model with this name
156158
if (method_exists($this->model, $column['name'])) {
157-
$column['entity'] = $column['name'];
159+
160+
// if it has parameters it's not a relation method.
161+
$column['entity'] = $this->modelMethodHasParameters($this->model, $column['name']) ? false : $column['name'];
158162

159163
return $column;
160164
}
@@ -165,7 +169,9 @@ protected function makeSureColumnHasEntity($column)
165169
$possibleMethodName = Str::replaceLast('_id', '', $column['name']);
166170

167171
if (method_exists($this->model, $possibleMethodName)) {
168-
$column['entity'] = $possibleMethodName;
172+
173+
// if it has parameters it's not a relation method.
174+
$column['entity'] = $this->modelMethodHasParameters($this->model, $possibleMethodName) ? false : $possibleMethodName;
169175

170176
return $column;
171177
}

0 commit comments

Comments
 (0)