Skip to content

Commit

Permalink
Merge pull request #7 from RemiHin/main
Browse files Browse the repository at this point in the history
Add a scope option to the query
  • Loading branch information
vildanbina authored Sep 19, 2024
2 parents 12ed75a + f90f7ff commit 16d0be7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/ExportModelData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -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'],
];
}
}
21 changes: 21 additions & 0 deletions src/Traits/HasModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,6 +20,11 @@ trait HasModel
*/
protected string $model;

/**
* @var ?string
*/
protected ?string $scope = null;

/**
* @param string $model
*
Expand All @@ -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
*/
Expand All @@ -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();
});
}
}

0 comments on commit 16d0be7

Please sign in to comment.