From bcc2a8dade566d97693f6f51e5943c43e3c5abfd Mon Sep 17 00:00:00 2001 From: "marc@kjsoftware.nl" Date: Wed, 8 Feb 2023 20:45:19 +0100 Subject: [PATCH 1/8] ## Add - Support for column aliases over the AllowedFields --- src/AllowedField.php | 43 ++++++++++++++++++++++++++++++ src/Concerns/AddsFieldsToQuery.php | 33 +++++++++++++---------- src/Mappings/Column.php | 18 +++++++++++++ src/QueryBuilderRequest.php | 10 +++---- 4 files changed, 85 insertions(+), 19 deletions(-) create mode 100644 src/AllowedField.php create mode 100644 src/Mappings/Column.php diff --git a/src/AllowedField.php b/src/AllowedField.php new file mode 100644 index 00000000..d5b9a3be --- /dev/null +++ b/src/AllowedField.php @@ -0,0 +1,43 @@ +name = $name; + + $this->internalName = $internalName ?? $name; + } + + + public static function setFilterArrayValueDelimiter(string $delimiter = null): void + { + if (isset($delimiter)) { + QueryBuilderRequest::setFilterArrayValueDelimiter($delimiter); + } + } + + public static function partial(string $name, $internalName = null, bool $addRelationConstraint = true, string $arrayValueDelimiter = null): self + { + static::setFilterArrayValueDelimiter($arrayValueDelimiter); + return new static($name, $internalName); + } + + public function getName(): string + { + return $this->name; + } + public function getInternalName(): string + { + return $this->internalName; + } +} diff --git a/src/Concerns/AddsFieldsToQuery.php b/src/Concerns/AddsFieldsToQuery.php index 63cd8ad4..67a479ba 100644 --- a/src/Concerns/AddsFieldsToQuery.php +++ b/src/Concerns/AddsFieldsToQuery.php @@ -4,6 +4,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\Str; +use Spatie\QueryBuilder\AllowedField; use Spatie\QueryBuilder\Exceptions\AllowedFieldsMustBeCalledBeforeAllowedIncludes; use Spatie\QueryBuilder\Exceptions\InvalidFieldQuery; use Spatie\QueryBuilder\Exceptions\UnknownIncludedFieldsQuery; @@ -20,10 +21,13 @@ public function allowedFields($fields): static $fields = is_array($fields) ? $fields : func_get_args(); - $this->allowedFields = collect($fields) - ->map(function (string $fieldName) { - return $this->prependField($fieldName); - }); + $this->allowedFields = collect($fields)->map(function ($field) { + if ($field instanceof AllowedField) { + return $field; + } + + return AllowedField::partial($field); + }); $this->ensureAllFieldsExist(); @@ -36,7 +40,10 @@ protected function addRequestedModelFieldsToQuery() { $modelTableName = $this->getModel()->getTable(); - $modelFields = $this->request->fields()->get($modelTableName); + $this->allowedFields->map(function (AllowedField $field) { + if ($this->request->fields()->where('name', $field->getName())->count() > 0) + return $field->getInternalName(); + })->toArray(); if (empty($modelFields)) { return; @@ -70,19 +77,17 @@ public function getRequestedFieldsForRelatedTable(string $relation): array protected function ensureAllFieldsExist() { - $requestedFields = $this->request->fields() - ->map(function ($fields, $model) { - $tableName = $model; + // Map fieldnames from object + $allowedFields = $this->allowedFields->map(function (AllowedField $field) { + return $field->getName(); + }); - return $this->prependFieldsWithTableName($fields, $tableName); - }) - ->flatten() - ->unique(); + $requestedFields = $this->request->fields(); - $unknownFields = $requestedFields->diff($this->allowedFields); + $unknownFields = $requestedFields->pluck('name')->diff($allowedFields); if ($unknownFields->isNotEmpty()) { - throw InvalidFieldQuery::fieldsNotAllowed($unknownFields, $this->allowedFields); + throw InvalidFieldQuery::fieldsNotAllowed($unknownFields, $allowedFields); } } diff --git a/src/Mappings/Column.php b/src/Mappings/Column.php new file mode 100644 index 00000000..2da8980d --- /dev/null +++ b/src/Mappings/Column.php @@ -0,0 +1,18 @@ +name = $name; + } + +} diff --git a/src/QueryBuilderRequest.php b/src/QueryBuilderRequest.php index f0eded93..519b1b9a 100644 --- a/src/QueryBuilderRequest.php +++ b/src/QueryBuilderRequest.php @@ -5,6 +5,7 @@ use Illuminate\Http\Request; use Illuminate\Support\Collection; use Illuminate\Support\Str; +use Spatie\QueryBuilder\Mappings\Column; class QueryBuilderRequest extends Request { @@ -62,15 +63,14 @@ public function fields(): Collection { $fieldsParameterName = config('query-builder.parameters.fields'); - $fieldsPerTable = collect($this->getRequestData($fieldsParameterName)); - if ($fieldsPerTable->isEmpty()) { + $data = $this->getRequestData($fieldsParameterName); + + if (!$data) { return collect(); } - return $fieldsPerTable->map(function ($fields) { - return explode(static::getFieldsArrayValueDelimiter(), $fields); - }); + return collect(explode(static::getFieldsArrayValueDelimiter(), $data))->mapInto(Column::class); } public function sorts(): Collection From 806e996266923709aa51f3626ba93d5d4ea639a1 Mon Sep 17 00:00:00 2001 From: Ruud Deenen Date: Wed, 15 Nov 2023 17:46:17 +0100 Subject: [PATCH 2/8] SV-401 --- src/Concerns/AddsFieldsToQuery.php | 14 ++++++++++---- src/Concerns/FiltersQuery.php | 6 ++++++ src/Concerns/SortsQuery.php | 17 ++++++++++++----- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/Concerns/AddsFieldsToQuery.php b/src/Concerns/AddsFieldsToQuery.php index 67a479ba..b10c5e66 100644 --- a/src/Concerns/AddsFieldsToQuery.php +++ b/src/Concerns/AddsFieldsToQuery.php @@ -41,8 +41,9 @@ protected function addRequestedModelFieldsToQuery() $modelTableName = $this->getModel()->getTable(); $this->allowedFields->map(function (AllowedField $field) { - if ($this->request->fields()->where('name', $field->getName())->count() > 0) + if ($this->request->fields()->where('name', $field->getName())->count() > 0) { return $field->getInternalName(); + } })->toArray(); if (empty($modelFields)) { @@ -62,11 +63,11 @@ public function getRequestedFieldsForRelatedTable(string $relation): array return [$table => $fields]; })->get($table); - if (! $fields) { + if (!$fields) { return []; } - if (! $this->allowedFields instanceof Collection) { + if (!$this->allowedFields instanceof Collection) { // We have requested fields but no allowed fields (yet?) throw new UnknownIncludedFieldsQuery($fields); @@ -100,7 +101,7 @@ protected function prependFieldsWithTableName(array $fields, string $tableName): protected function prependField(string $field, ?string $table = null): string { - if (! $table) { + if (!$table) { $table = $this->getModel()->getTable(); } @@ -112,4 +113,9 @@ protected function prependField(string $field, ?string $table = null): string return "{$table}.{$field}"; } + + public function getAllowedFields(): ?Collection + { + return $this->allowedFields; + } } diff --git a/src/Concerns/FiltersQuery.php b/src/Concerns/FiltersQuery.php index 8361df06..b62a78e7 100644 --- a/src/Concerns/FiltersQuery.php +++ b/src/Concerns/FiltersQuery.php @@ -2,6 +2,7 @@ namespace Spatie\QueryBuilder\Concerns; +use Illuminate\Support\Collection; use Spatie\QueryBuilder\AllowedFilter; use Spatie\QueryBuilder\Exceptions\InvalidFilterQuery; @@ -78,4 +79,9 @@ protected function ensureAllFiltersExist() throw InvalidFilterQuery::filtersNotAllowed($diff, $allowedFilterNames); } } + + public function getAllowedFilters(): ?Collection + { + return $this->allowedFilters; + } } diff --git a/src/Concerns/SortsQuery.php b/src/Concerns/SortsQuery.php index f8adf89d..f6a72fb9 100644 --- a/src/Concerns/SortsQuery.php +++ b/src/Concerns/SortsQuery.php @@ -2,12 +2,14 @@ namespace Spatie\QueryBuilder\Concerns; +use Illuminate\Support\Collection; use Spatie\QueryBuilder\AllowedSort; use Spatie\QueryBuilder\Exceptions\InvalidSortQuery; +use Spatie\QueryBuilder\QueryBuilder; trait SortsQuery { - /** @var \Illuminate\Support\Collection */ + /** @var Collection */ protected $allowedSorts; public function allowedSorts($sorts): static @@ -36,9 +38,9 @@ public function allowedSorts($sorts): static } /** - * @param array|string|\Spatie\QueryBuilder\AllowedSort $sorts + * @param array|string|AllowedSort $sorts * - * @return \Spatie\QueryBuilder\QueryBuilder + * @return QueryBuilder */ public function defaultSort($sorts): static { @@ -46,9 +48,9 @@ public function defaultSort($sorts): static } /** - * @param array|string|\Spatie\QueryBuilder\AllowedSort $sorts + * @param array|string|AllowedSort $sorts * - * @return \Spatie\QueryBuilder\QueryBuilder + * @return QueryBuilder */ public function defaultSorts($sorts): static { @@ -113,4 +115,9 @@ protected function ensureAllSortsExist(): void throw InvalidSortQuery::sortsNotAllowed($unknownSorts, $allowedSortNames); } } + + public function getAllowedSorts(): ?Collection + { + return $this->allowedSorts; + } } From 26b6818893740ca4646857514b47b5a453bae9a9 Mon Sep 17 00:00:00 2001 From: Ruud Deenen Date: Mon, 5 Feb 2024 16:56:34 +0100 Subject: [PATCH 3/8] SV-401 --- README.md | 11 +++ composer.json | 3 +- src/App/Console/Commands/CacheForeignKeys.php | 72 +++++++++++++++++++ src/QueryBuilderServiceProvider.php | 2 + 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/App/Console/Commands/CacheForeignKeys.php diff --git a/README.md b/README.md index 2e041f11..f065e80a 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,17 @@ This package allows you to filter, sort and include eloquent relations based on a request. The `QueryBuilder` used in this package extends Laravel's default Eloquent builder. This means all your favorite methods and macros are still available. Query parameter names follow the [JSON API specification](http://jsonapi.org/) as closely as possible. +## Caching foreign keys +Add this line to your composer.json file to cache foreign keys. This will allow the query builder to automatically detect foreign keys without having to make a database call. +These foreign keys will always be included in select statements, which will prevent a loss of potential relations. +```json +"scripts": { + "post-update-cmd": [ + "@php artisan query-builder:cache-foreign-keys" + ], +} +``` + ## Basic usage ### Filter a query based on a request: `/users?filter[name]=John`: diff --git a/composer.json b/composer.json index 35d18f37..942479e3 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,8 @@ "illuminate/database": "^9.0|^10.0", "illuminate/http": "^9.0|^10.0", "illuminate/support": "^9.0|^10.0", - "spatie/laravel-package-tools": "^1.11" + "spatie/laravel-package-tools": "^1.11", + "doctrine/dbal": "^3.5" }, "require-dev": { "ext-json": "*", diff --git a/src/App/Console/Commands/CacheForeignKeys.php b/src/App/Console/Commands/CacheForeignKeys.php new file mode 100644 index 00000000..c7e628a5 --- /dev/null +++ b/src/App/Console/Commands/CacheForeignKeys.php @@ -0,0 +1,72 @@ +info('Fetching all models in App\Models...' . PHP_EOL); + + foreach ($files as $file) { + // Build the full class name + $fullClassName = 'App\\Models' . '\\' . $file->getRelativePath() . '\\' . pathinfo($file->getRelativePathname(), PATHINFO_FILENAME); + $fullClassName = Str::replace('/', '\\', $fullClassName); + + // Check if the class exists + if (class_exists($fullClassName) && is_subclass_of($fullClassName, Model::class)) { + // Create a ReflectionClass instance + $reflectionClass = new ReflectionClass($fullClassName); + + // Check if the class is instantiable + if ($reflectionClass->isInstantiable()) { + // Instantiate the class + $instance = $reflectionClass->newInstance(); + $table = $instance->getTable(); + + // Get all foreign keys for the table + $tableForeignKeys = Schema::getConnection()->getDoctrineSchemaManager()->listTableForeignKeys($table); + + // Add the foreign keys to the array + $foreignKeys[$table] = array_reduce($tableForeignKeys, function ($carry, $foreignKey) { + return array_merge($carry, $foreignKey->getLocalColumns()); + }, []); + + // Add the primary key to the array + $foreignKeys[$table][] = $instance->getKeyName(); + } else { + $this->error("The class $fullClassName is not instantiable."); + } + } else { + $this->warn("The class $fullClassName does not exist or does not extend " . Model::class . '.'); + } + } + $this->info(PHP_EOL . 'Cached foreign keys for ' . count($foreignKeys) . ' tables.'); + + Cache::forever('QUERY_BUILDER_FKS', $foreignKeys); + } + + public static function getForTable(string $table): array + { + return Cache::get('QUERY_BUILDER_FKS')[$table] ?? []; + } + +} diff --git a/src/QueryBuilderServiceProvider.php b/src/QueryBuilderServiceProvider.php index c7e7b199..7a989c38 100644 --- a/src/QueryBuilderServiceProvider.php +++ b/src/QueryBuilderServiceProvider.php @@ -4,6 +4,7 @@ use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; +use Spatie\QueryBuilder\App\Console\Commands\CacheForeignKeys; class QueryBuilderServiceProvider extends PackageServiceProvider { @@ -11,6 +12,7 @@ public function configurePackage(Package $package): void { $package ->name('laravel-query-builder') + ->hasConsoleCommands(CacheForeignKeys::class) ->hasConfigFile(); } From 49a451123bab44ceec6a036614c2b4bdba9be67d Mon Sep 17 00:00:00 2001 From: Ruud Deenen Date: Wed, 27 Mar 2024 09:51:24 +0100 Subject: [PATCH 4/8] SV-401 --- src/AllowedField.php | 20 +++++++-------- src/Concerns/AddsFieldsToQuery.php | 39 +++++++++++++++++++++--------- src/Concerns/SortsQuery.php | 6 ----- 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/AllowedField.php b/src/AllowedField.php index d5b9a3be..0972008d 100644 --- a/src/AllowedField.php +++ b/src/AllowedField.php @@ -2,20 +2,19 @@ namespace Spatie\QueryBuilder; +use Illuminate\Support\Collection; use Spatie\QueryBuilder\Filters\Filter; class AllowedField { - /** @var string */ - protected $name; + protected string $name; + protected Collection $internalNames; - /** @var string */ - protected $internalName; - public function __construct(string $name, ?string $internalName = null) + public function __construct(string $name, string|array $internalName = null) { $this->name = $name; - $this->internalName = $internalName ?? $name; + $this->internalNames = collect($internalName); } @@ -26,18 +25,19 @@ public static function setFilterArrayValueDelimiter(string $delimiter = null): v } } - public static function partial(string $name, $internalName = null, bool $addRelationConstraint = true, string $arrayValueDelimiter = null): self + public static function partial(string $name, $internalNames = null, bool $addRelationConstraint = true, string $arrayValueDelimiter = null): self { static::setFilterArrayValueDelimiter($arrayValueDelimiter); - return new static($name, $internalName); + return new static($name, $internalNames); } public function getName(): string { return $this->name; } - public function getInternalName(): string + + public function getInternalNames(): Collection { - return $this->internalName; + return $this->internalNames; } } diff --git a/src/Concerns/AddsFieldsToQuery.php b/src/Concerns/AddsFieldsToQuery.php index b10c5e66..42daf1b5 100644 --- a/src/Concerns/AddsFieldsToQuery.php +++ b/src/Concerns/AddsFieldsToQuery.php @@ -5,13 +5,14 @@ use Illuminate\Support\Collection; use Illuminate\Support\Str; use Spatie\QueryBuilder\AllowedField; +use Spatie\QueryBuilder\App\Console\Commands\CacheForeignKeys; use Spatie\QueryBuilder\Exceptions\AllowedFieldsMustBeCalledBeforeAllowedIncludes; use Spatie\QueryBuilder\Exceptions\InvalidFieldQuery; use Spatie\QueryBuilder\Exceptions\UnknownIncludedFieldsQuery; trait AddsFieldsToQuery { - protected ?Collection $allowedFields = null; + public ?Collection $allowedFields = null; public function allowedFields($fields): static { @@ -36,20 +37,38 @@ public function allowedFields($fields): static return $this; } - protected function addRequestedModelFieldsToQuery() + protected function addRequestedModelFieldsToQuery(): void { $modelTableName = $this->getModel()->getTable(); - $this->allowedFields->map(function (AllowedField $field) { - if ($this->request->fields()->where('name', $field->getName())->count() > 0) { - return $field->getInternalName(); - } - })->toArray(); + $requestFields = $this->request->fields()->map(function ($field) { + return $field->name; + }); + + $modelFields = $this->allowedFields->mapWithKeys(function (AllowedField $field) { + return [ + $field->getName() => $field->getInternalNames()->toArray() + ]; + }); + + if ($requestFields->count() > 0) { + // If fields are requested, only select those + $modelFields = $modelFields->filter(function ($internalName, $name) use ($requestFields) { + return $requestFields->contains($name); + })->toArray(); + } else { + // If no fields are requested, select all allowed fields + $modelFields = $modelFields->toArray(); + } if (empty($modelFields)) { return; } + // Flatten array + $modelFields = array_unique(array_merge(...array_values($modelFields))); + + // Prepend the fields with the table name $prependedFields = $this->prependFieldsWithTableName($modelFields, $modelTableName); $this->select($prependedFields); @@ -57,7 +76,7 @@ protected function addRequestedModelFieldsToQuery() public function getRequestedFieldsForRelatedTable(string $relation): array { - $table = Str::plural(Str::snake($relation)); // TODO: make this configurable + $table = Str::plural(Str::snake($relation)); $fields = $this->request->fields()->mapWithKeys(function ($fields, $table) { return [$table => $fields]; @@ -69,14 +88,13 @@ public function getRequestedFieldsForRelatedTable(string $relation): array if (!$this->allowedFields instanceof Collection) { // We have requested fields but no allowed fields (yet?) - throw new UnknownIncludedFieldsQuery($fields); } return $fields; } - protected function ensureAllFieldsExist() + protected function ensureAllFieldsExist(): void { // Map fieldnames from object $allowedFields = $this->allowedFields->map(function (AllowedField $field) { @@ -107,7 +125,6 @@ protected function prependField(string $field, ?string $table = null): string if (Str::contains($field, '.')) { // Already prepended - return $field; } diff --git a/src/Concerns/SortsQuery.php b/src/Concerns/SortsQuery.php index f6a72fb9..2f42b3f4 100644 --- a/src/Concerns/SortsQuery.php +++ b/src/Concerns/SortsQuery.php @@ -14,12 +14,6 @@ trait SortsQuery public function allowedSorts($sorts): static { - if ($this->request->sorts()->isEmpty()) { - // We haven't got any requested sorts. No need to parse allowed sorts. - - return $this; - } - $sorts = is_array($sorts) ? $sorts : func_get_args(); $this->allowedSorts = collect($sorts)->map(function ($sort) { From 2e7a5a65c30d85bfe967b70167e215d1f67fe103 Mon Sep 17 00:00:00 2001 From: KJ-Quinn Date: Wed, 12 Jun 2024 13:55:45 +0000 Subject: [PATCH 5/8] Fix styling --- src/AllowedField.php | 2 +- src/App/Console/Commands/CacheForeignKeys.php | 1 - src/Concerns/AddsFieldsToQuery.php | 9 ++++----- src/Concerns/SortsQuery.php | 1 - src/Mappings/Column.php | 2 +- src/QueryBuilderRequest.php | 2 +- 6 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/AllowedField.php b/src/AllowedField.php index 0972008d..2be79d1d 100644 --- a/src/AllowedField.php +++ b/src/AllowedField.php @@ -3,7 +3,6 @@ namespace Spatie\QueryBuilder; use Illuminate\Support\Collection; -use Spatie\QueryBuilder\Filters\Filter; class AllowedField { @@ -28,6 +27,7 @@ public static function setFilterArrayValueDelimiter(string $delimiter = null): v public static function partial(string $name, $internalNames = null, bool $addRelationConstraint = true, string $arrayValueDelimiter = null): self { static::setFilterArrayValueDelimiter($arrayValueDelimiter); + return new static($name, $internalNames); } diff --git a/src/App/Console/Commands/CacheForeignKeys.php b/src/App/Console/Commands/CacheForeignKeys.php index c7e628a5..45934492 100644 --- a/src/App/Console/Commands/CacheForeignKeys.php +++ b/src/App/Console/Commands/CacheForeignKeys.php @@ -12,7 +12,6 @@ class CacheForeignKeys extends Command { - protected $signature = 'query-builder:cache-foreign-keys'; protected $description = 'Cache foreign keys for the QueryBuilder package.'; diff --git a/src/Concerns/AddsFieldsToQuery.php b/src/Concerns/AddsFieldsToQuery.php index dc1137d3..726574d1 100644 --- a/src/Concerns/AddsFieldsToQuery.php +++ b/src/Concerns/AddsFieldsToQuery.php @@ -5,7 +5,6 @@ use Illuminate\Support\Collection; use Illuminate\Support\Str; use Spatie\QueryBuilder\AllowedField; -use Spatie\QueryBuilder\App\Console\Commands\CacheForeignKeys; use Spatie\QueryBuilder\Exceptions\AllowedFieldsMustBeCalledBeforeAllowedIncludes; use Spatie\QueryBuilder\Exceptions\InvalidFieldQuery; use Spatie\QueryBuilder\Exceptions\UnknownIncludedFieldsQuery; @@ -47,7 +46,7 @@ protected function addRequestedModelFieldsToQuery(): void $modelFields = $this->allowedFields->mapWithKeys(function (AllowedField $field) { return [ - $field->getName() => $field->getInternalNames()->toArray() + $field->getName() => $field->getInternalNames()->toArray(), ]; }); @@ -84,11 +83,11 @@ public function getRequestedFieldsForRelatedTable(string $relation): array ->mapWithKeys(fn ($fields, $table) => [$table => $fields]) ->get($tableOrRelation); - if (!$fields) { + if (! $fields) { return []; } - if (!$this->allowedFields instanceof Collection) { + if (! $this->allowedFields instanceof Collection) { // We have requested fields but no allowed fields (yet?) throw new UnknownIncludedFieldsQuery($fields); } @@ -121,7 +120,7 @@ protected function prependFieldsWithTableName(array $fields, string $tableName): protected function prependField(string $field, ?string $table = null): string { - if (!$table) { + if (! $table) { $table = $this->getModel()->getTable(); } diff --git a/src/Concerns/SortsQuery.php b/src/Concerns/SortsQuery.php index acce9e65..15dded43 100644 --- a/src/Concerns/SortsQuery.php +++ b/src/Concerns/SortsQuery.php @@ -5,7 +5,6 @@ use Illuminate\Support\Collection; use Spatie\QueryBuilder\AllowedSort; use Spatie\QueryBuilder\Exceptions\InvalidSortQuery; -use Spatie\QueryBuilder\QueryBuilder; trait SortsQuery { diff --git a/src/Mappings/Column.php b/src/Mappings/Column.php index 2da8980d..3a582c11 100644 --- a/src/Mappings/Column.php +++ b/src/Mappings/Column.php @@ -10,7 +10,7 @@ class Column * @param string $name * @return void */ - function __construct(string $name) + public function __construct(string $name) { $this->name = $name; } diff --git a/src/QueryBuilderRequest.php b/src/QueryBuilderRequest.php index 78e4dd44..ea500416 100644 --- a/src/QueryBuilderRequest.php +++ b/src/QueryBuilderRequest.php @@ -68,7 +68,7 @@ public function fields(): Collection $data = $this->getRequestData($fieldsParameterName); - if (!$data) { + if (! $data) { return collect(); } From 387952c82f3195b42eb502c4ba23d03ba573d028 Mon Sep 17 00:00:00 2001 From: KJ-Quinn <89981796+KJ-Quinn@users.noreply.github.com> Date: Thu, 11 Jul 2024 14:57:05 +0200 Subject: [PATCH 6/8] Update composer.json --- composer.json | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index e7ce390f..ddca61f3 100644 --- a/composer.json +++ b/composer.json @@ -1,24 +1,11 @@ { - "name": "spatie/laravel-query-builder", + "name": "kjsoftware/laravel-query-builder", "description": "Easily build Eloquent queries from API requests", "keywords": [ - "spatie", + "kjsoftware", "laravel-query-builder" ], - "homepage": "https://github.com/spatie/laravel-query-builder", "license": "MIT", - "support": { - "issues": "https://github.com/spatie/laravel-query-builder/issues", - "source": "https://github.com/spatie/laravel-query-builder" - }, - "authors": [ - { - "name": "Alex Vanderbist", - "email": "alex@spatie.be", - "homepage": "https://spatie.be", - "role": "Developer" - } - ], "require": { "php": "^8.2", "illuminate/database": "^10.0|^11.0", From e535a1e9731983f3f7a553d2c7e584657b5cdad4 Mon Sep 17 00:00:00 2001 From: KJ-Quinn <89981796+KJ-Quinn@users.noreply.github.com> Date: Fri, 12 Jul 2024 10:55:43 +0200 Subject: [PATCH 7/8] Update composer.json --- composer.json | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index ddca61f3..e7ce390f 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,24 @@ { - "name": "kjsoftware/laravel-query-builder", + "name": "spatie/laravel-query-builder", "description": "Easily build Eloquent queries from API requests", "keywords": [ - "kjsoftware", + "spatie", "laravel-query-builder" ], + "homepage": "https://github.com/spatie/laravel-query-builder", "license": "MIT", + "support": { + "issues": "https://github.com/spatie/laravel-query-builder/issues", + "source": "https://github.com/spatie/laravel-query-builder" + }, + "authors": [ + { + "name": "Alex Vanderbist", + "email": "alex@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], "require": { "php": "^8.2", "illuminate/database": "^10.0|^11.0", From 56c9005ebb1d8a8246a3f23839dbfb24fb4bd635 Mon Sep 17 00:00:00 2001 From: KJ-Quinn Date: Fri, 12 Jul 2024 08:56:28 +0000 Subject: [PATCH 8/8] Fix styling --- src/AllowedField.php | 2 +- src/App/Console/Commands/CacheForeignKeys.php | 1 - src/Concerns/AddsFieldsToQuery.php | 9 ++++----- src/Mappings/Column.php | 2 +- src/QueryBuilderRequest.php | 2 +- 5 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/AllowedField.php b/src/AllowedField.php index 0972008d..2be79d1d 100644 --- a/src/AllowedField.php +++ b/src/AllowedField.php @@ -3,7 +3,6 @@ namespace Spatie\QueryBuilder; use Illuminate\Support\Collection; -use Spatie\QueryBuilder\Filters\Filter; class AllowedField { @@ -28,6 +27,7 @@ public static function setFilterArrayValueDelimiter(string $delimiter = null): v public static function partial(string $name, $internalNames = null, bool $addRelationConstraint = true, string $arrayValueDelimiter = null): self { static::setFilterArrayValueDelimiter($arrayValueDelimiter); + return new static($name, $internalNames); } diff --git a/src/App/Console/Commands/CacheForeignKeys.php b/src/App/Console/Commands/CacheForeignKeys.php index c7e628a5..45934492 100644 --- a/src/App/Console/Commands/CacheForeignKeys.php +++ b/src/App/Console/Commands/CacheForeignKeys.php @@ -12,7 +12,6 @@ class CacheForeignKeys extends Command { - protected $signature = 'query-builder:cache-foreign-keys'; protected $description = 'Cache foreign keys for the QueryBuilder package.'; diff --git a/src/Concerns/AddsFieldsToQuery.php b/src/Concerns/AddsFieldsToQuery.php index 42daf1b5..0ec3d340 100644 --- a/src/Concerns/AddsFieldsToQuery.php +++ b/src/Concerns/AddsFieldsToQuery.php @@ -5,7 +5,6 @@ use Illuminate\Support\Collection; use Illuminate\Support\Str; use Spatie\QueryBuilder\AllowedField; -use Spatie\QueryBuilder\App\Console\Commands\CacheForeignKeys; use Spatie\QueryBuilder\Exceptions\AllowedFieldsMustBeCalledBeforeAllowedIncludes; use Spatie\QueryBuilder\Exceptions\InvalidFieldQuery; use Spatie\QueryBuilder\Exceptions\UnknownIncludedFieldsQuery; @@ -47,7 +46,7 @@ protected function addRequestedModelFieldsToQuery(): void $modelFields = $this->allowedFields->mapWithKeys(function (AllowedField $field) { return [ - $field->getName() => $field->getInternalNames()->toArray() + $field->getName() => $field->getInternalNames()->toArray(), ]; }); @@ -82,11 +81,11 @@ public function getRequestedFieldsForRelatedTable(string $relation): array return [$table => $fields]; })->get($table); - if (!$fields) { + if (! $fields) { return []; } - if (!$this->allowedFields instanceof Collection) { + if (! $this->allowedFields instanceof Collection) { // We have requested fields but no allowed fields (yet?) throw new UnknownIncludedFieldsQuery($fields); } @@ -119,7 +118,7 @@ protected function prependFieldsWithTableName(array $fields, string $tableName): protected function prependField(string $field, ?string $table = null): string { - if (!$table) { + if (! $table) { $table = $this->getModel()->getTable(); } diff --git a/src/Mappings/Column.php b/src/Mappings/Column.php index 2da8980d..3a582c11 100644 --- a/src/Mappings/Column.php +++ b/src/Mappings/Column.php @@ -10,7 +10,7 @@ class Column * @param string $name * @return void */ - function __construct(string $name) + public function __construct(string $name) { $this->name = $name; } diff --git a/src/QueryBuilderRequest.php b/src/QueryBuilderRequest.php index 519b1b9a..d2395644 100644 --- a/src/QueryBuilderRequest.php +++ b/src/QueryBuilderRequest.php @@ -66,7 +66,7 @@ public function fields(): Collection $data = $this->getRequestData($fieldsParameterName); - if (!$data) { + if (! $data) { return collect(); }