Add custom collection support for get and all methods (#903)

* Add custom collection support for get and all methods

* Only add get and all when using custom collection

* Use static instead of class name

* Add missing custom collection test with relation

* Use class reference over string

Co-Authored-By: Markus Podar <[email protected]>

* Fix when using class reference

Co-authored-by: Markus Podar <[email protected]>
This commit is contained in:
Daniel Mason
2020-04-19 07:49:23 +02:00
committed by GitHub
co-authored by Markus Podar
parent 18588eacf3
commit 6a27c5df68
4 changed files with 140 additions and 0 deletions
+16
View File
@@ -12,6 +12,7 @@ namespace Barryvdh\LaravelIdeHelper\Console;
use Composer\Autoload\ClassMapGenerator;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Str;
use Illuminate\Filesystem\Filesystem;
@@ -229,6 +230,7 @@ class ModelsCommand extends Command
$this->getPropertiesFromMethods($model);
$this->getSoftDeleteMethods($model);
$this->getCollectionMethods($model);
$output .= $this->createPhpDocs($name);
$ignore[] = $name;
$this->nullableColumns = [];
@@ -850,6 +852,20 @@ class ModelsCommand extends Command
}
}
/**
* Generates methods that return collections
* @param \Illuminate\Database\Eloquent\Model $model
*/
protected function getCollectionMethods($model)
{
$collectionClass = $this->getCollectionClass(get_class($model));
if ($collectionClass !== '\\' . \Illuminate\Database\Eloquent\Collection::class) {
$this->setMethod('get', $collectionClass . '|static[]', ['$columns = [\'*\']']);
$this->setMethod('all', $collectionClass . '|static[]', ['$columns = [\'*\']']);
}
}
/**
* @param ReflectionClass $reflection
* @return string