From 3cb36a24142148434278727fd0078368031ffdf9 Mon Sep 17 00:00:00 2001 From: Remi Hindriks <56312208+RemiHin@users.noreply.github.com> Date: Tue, 10 Sep 2024 11:20:58 +0200 Subject: [PATCH 1/4] added the scope option --- README.md | 17 +++++++++++++++++ src/Commands/ExportModelData.php | 2 ++ src/Traits/HasModel.php | 25 +++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/README.md b/README.md index 7fcac1a..2f8f974 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,23 @@ If a model has a large number of columns and you only want to export a subset of php artisan model:export User --only-fields=name,email ``` +### Apply a specific scope to the query + +If you wish to apply a scope to the model query because you wish to exclude certain records, you can use the `--scope={scope}` option. This allows you to specify a scope for the records you want to include in the export. For example: + +```bash +php artisan model:export User --scope=verified +``` + +On your User Model you would have the following function: + +```php + public function scopeVerified(Builder $query): void + { + $query->whereNotNull('email_verified_at'); + } +``` + ### Relationships You can now export models along with their specified relationships using the new option `--with-relationships={relations}`. `{relations}` are the names of the relationships and can be separated by `+` symbol if you want to attach more than one relationship. diff --git a/src/Commands/ExportModelData.php b/src/Commands/ExportModelData.php index 0fdaf83..5b4a8af 100644 --- a/src/Commands/ExportModelData.php +++ b/src/Commands/ExportModelData.php @@ -35,6 +35,7 @@ public function handle() $exportService = ExportService::make() ->setModel($modelClass) + ->setScope($this->option('scope')) ->setFilename($this->option('filename')) ->setPath($this->option('path')) ->setExceptColumns($this->option('except-fields')) @@ -79,6 +80,7 @@ protected function getOptions(): array ['without-timestamps', null, InputOption::VALUE_NONE, 'Export without: created_at, updated_at and deleted_at columns'], ['beautify', '-b', InputOption::VALUE_NONE, 'Beautify JSON'], ['with-relationships', null, InputOption::VALUE_OPTIONAL, 'Relationships to include (plus-separator)'], + ['scope', null, InputOption::VALUE_OPTIONAL, 'Scope you wish to apply to the query'], ]; } } diff --git a/src/Traits/HasModel.php b/src/Traits/HasModel.php index a4d98c2..41ba34a 100644 --- a/src/Traits/HasModel.php +++ b/src/Traits/HasModel.php @@ -2,6 +2,7 @@ namespace Vildanbina\ModelJson\Traits; +use http\Exception\BadMethodCallException; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; @@ -19,6 +20,11 @@ trait HasModel */ protected string $model; + /** + * @var ?string + */ + protected ?string $scope = null; + /** * @param string $model * @@ -39,6 +45,22 @@ public function setModel(string $model): static return $this; } + /** + * @param string $scope + * + * @return $this + */ + public function setScope(string $scope): static + { + $this->scope = $scope; + + if (!method_exists($this->model, 'scope'.ucfirst($this->scope))) { + throw new BadMethodCallException('Scope ' . $this->scope . ' does not exists.'); + } + + return $this; + } + /** * @return int */ @@ -55,6 +77,9 @@ protected function modelQuery(): Builder return $this->model::query() ->when(filled($relations = $this->getRelationships()), function (Builder $builder) use ($relations) { $builder->with($relations); + }) + ->when(!is_null($scope = $this->scope), function (Builder $builder) use ($scope) { + $builder->$scope(); }); } } From 080cbb96e593ac473d0c86a538b52c3598fd00c2 Mon Sep 17 00:00:00 2001 From: Remi Hindriks <56312208+RemiHin@users.noreply.github.com> Date: Tue, 10 Sep 2024 11:23:39 +0200 Subject: [PATCH 2/4] added scope option to exports --- src/Traits/HasModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/HasModel.php b/src/Traits/HasModel.php index 41ba34a..8a4ffda 100644 --- a/src/Traits/HasModel.php +++ b/src/Traits/HasModel.php @@ -2,7 +2,7 @@ namespace Vildanbina\ModelJson\Traits; -use http\Exception\BadMethodCallException; +use BadMethodCallException; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; From 386e7ed1209e9b20fce3bc8541a2f3c52023b2cf Mon Sep 17 00:00:00 2001 From: Remi Hindriks <56312208+RemiHin@users.noreply.github.com> Date: Tue, 10 Sep 2024 11:26:07 +0200 Subject: [PATCH 3/4] added scope option to exports --- src/Traits/HasModel.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Traits/HasModel.php b/src/Traits/HasModel.php index 8a4ffda..e4ad4f9 100644 --- a/src/Traits/HasModel.php +++ b/src/Traits/HasModel.php @@ -46,15 +46,15 @@ public function setModel(string $model): static } /** - * @param string $scope + * @param ?string $scope * * @return $this */ - public function setScope(string $scope): static + public function setScope(?string $scope): static { $this->scope = $scope; - if (!method_exists($this->model, 'scope'.ucfirst($this->scope))) { + if (!is_null($this->scope) && !method_exists($this->model, 'scope'.ucfirst($this->scope))) { throw new BadMethodCallException('Scope ' . $this->scope . ' does not exists.'); } From f90f7ff8b4a3c037e6c29c776d6993e57b9d4026 Mon Sep 17 00:00:00 2001 From: Remi Hindriks <56312208+RemiHin@users.noreply.github.com> Date: Thu, 19 Sep 2024 14:56:36 +0200 Subject: [PATCH 4/4] remove exception and fixed is_null to filled --- src/Traits/HasModel.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Traits/HasModel.php b/src/Traits/HasModel.php index e4ad4f9..10a5aa4 100644 --- a/src/Traits/HasModel.php +++ b/src/Traits/HasModel.php @@ -54,10 +54,6 @@ public function setScope(?string $scope): static { $this->scope = $scope; - if (!is_null($this->scope) && !method_exists($this->model, 'scope'.ucfirst($this->scope))) { - throw new BadMethodCallException('Scope ' . $this->scope . ' does not exists.'); - } - return $this; } @@ -78,7 +74,7 @@ protected function modelQuery(): Builder ->when(filled($relations = $this->getRelationships()), function (Builder $builder) use ($relations) { $builder->with($relations); }) - ->when(!is_null($scope = $this->scope), function (Builder $builder) use ($scope) { + ->when(filled($scope = $this->scope), function (Builder $builder) use ($scope) { $builder->$scope(); }); }