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..10a5aa4 100644 --- a/src/Traits/HasModel.php +++ b/src/Traits/HasModel.php @@ -2,6 +2,7 @@ namespace Vildanbina\ModelJson\Traits; +use 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,18 @@ public function setModel(string $model): static return $this; } + /** + * @param ?string $scope + * + * @return $this + */ + public function setScope(?string $scope): static + { + $this->scope = $scope; + + return $this; + } + /** * @return int */ @@ -55,6 +73,9 @@ protected function modelQuery(): Builder return $this->model::query() ->when(filled($relations = $this->getRelationships()), function (Builder $builder) use ($relations) { $builder->with($relations); + }) + ->when(filled($scope = $this->scope), function (Builder $builder) use ($scope) { + $builder->$scope(); }); } }