From 9928f3735215cb74a07e03689f91d7b6824daaf7 Mon Sep 17 00:00:00 2001 From: Adam Anderly Date: Tue, 6 Aug 2019 10:03:23 -0500 Subject: [PATCH 1/8] Update SortRelations.php --- src/SortRelations.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/SortRelations.php b/src/SortRelations.php index 85fc8eb..3b5e35a 100644 --- a/src/SortRelations.php +++ b/src/SortRelations.php @@ -2,6 +2,8 @@ namespace LifeOnScreen\SortRelations; +use Illuminate\Support\Str; + /** * Trait SortRelations * @package LifeOnScreen\SortRelations @@ -37,7 +39,7 @@ protected static function applyRelationOrderings(string $column, string $directi $foreignKey = Str::snake($relation) . '_' . $related->getKeyName(); $query->select($model->getTable() . '.*'); - $query->join($related->getTable(), $model->qualifyColumn($foreignKey), '=', $related->qualifyColumn($related->getKeyName())); + $query->leftJoin($related->getTable(), $model->qualifyColumn($foreignKey), '=', $related->qualifyColumn($related->getKeyName())); if (is_string($sortRelations[$column])) { $qualified = $related->qualifyColumn($sortRelations[$column]); $query->orderBy($qualified, $direction); From 10ab6e7abdcc84b82c9cf45972a439765c31fe82 Mon Sep 17 00:00:00 2001 From: Newton Evangelista da Gama Junior Date: Sun, 17 May 2020 15:16:09 -0300 Subject: [PATCH 2/8] Default direction is 'asc' --- src/SortRelations.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/SortRelations.php b/src/SortRelations.php index 3b5e35a..54635d8 100644 --- a/src/SortRelations.php +++ b/src/SortRelations.php @@ -71,6 +71,8 @@ protected static function applyOrderings($query, array $orderings) $sortRelations = static::sortableRelations(); foreach ($orderings as $column => $direction) { + if (is_null($direction)) + $direction = 'asc'; if (array_key_exists($column, $sortRelations)) { $query = self::applyRelationOrderings($column, $direction, $query); } else { From 89bb69cf5ddf5dae32c5eb547c50cf5af7d30852 Mon Sep 17 00:00:00 2001 From: Newton Evangelista da Gama Junior Date: Sun, 17 May 2020 18:20:07 -0300 Subject: [PATCH 3/8] New structure generated by sortableRelations. Generated a new structure for sortableRelations output to identify the relation based on column name of the foreign key name. --- src/SortRelations.php | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/SortRelations.php b/src/SortRelations.php index 54635d8..06cf2df 100644 --- a/src/SortRelations.php +++ b/src/SortRelations.php @@ -15,9 +15,17 @@ trait SortRelations * * @return array */ - public static function sortableRelations(): array + public static function sortableRelations($query): array { - return static::$sortRelations ?? []; + $model = $query->getModel(); + $return = []; + if (static::$sortRelations) { + foreach (static::$sortRelation as $relation => $columns) { + $relatedKey = $model->{$relation}()->getForeignKeyName(); + $return[$relatedkey] = ['relation' => $relation, 'columns' => $columns]; + } + } + return $return; } /** @@ -30,22 +38,22 @@ public static function sortableRelations(): array */ protected static function applyRelationOrderings(string $column, string $direction, $query) { - $sortRelations = static::sortableRelations(); + $sortRelations = static::sortableRelations($query); $model = $query->getModel(); - $relation = $column; - $related = $model->{$column}()->getRelated(); + $relation = $sortRelations[$column]; + $related = $model->{$relation['relation']}()->getRelated(); - $foreignKey = Str::snake($relation) . '_' . $related->getKeyName(); + $foreignKey = Str::snake($relation['relation']) . '_' . $related->getKeyName(); $query->select($model->getTable() . '.*'); $query->leftJoin($related->getTable(), $model->qualifyColumn($foreignKey), '=', $related->qualifyColumn($related->getKeyName())); - if (is_string($sortRelations[$column])) { - $qualified = $related->qualifyColumn($sortRelations[$column]); + if (is_string($relation['columns'])) { + $qualified = $related->qualifyColumn($relation['columns']); $query->orderBy($qualified, $direction); } - if (is_array($sortRelations[$column])) { - foreach ($sortRelations[$column] as $orderColumn) { + if (is_array($relation['columns'])) { + foreach ($relation['columns'] as $orderColumn) { $query->orderBy($related->qualifyColumn($orderColumn), $direction); } } @@ -68,7 +76,7 @@ protected static function applyOrderings($query, array $orderings) : $query; } - $sortRelations = static::sortableRelations(); + $sortRelations = static::sortableRelations($query); foreach ($orderings as $column => $direction) { if (is_null($direction)) From 91019e66b93727e7ad7f03bac18d0838ba403b76 Mon Sep 17 00:00:00 2001 From: Newton Evangelista da Gama Junior Date: Sun, 17 May 2020 18:43:02 -0300 Subject: [PATCH 4/8] Adjustments to complete new sortableRelations For better comprehension, foreignKey and ownerKey are obtained from BelongsTo class methods and $qualified is used on both string and array form. --- src/SortRelations.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/SortRelations.php b/src/SortRelations.php index 06cf2df..a65746e 100644 --- a/src/SortRelations.php +++ b/src/SortRelations.php @@ -44,17 +44,19 @@ protected static function applyRelationOrderings(string $column, string $directi $relation = $sortRelations[$column]; $related = $model->{$relation['relation']}()->getRelated(); - $foreignKey = Str::snake($relation['relation']) . '_' . $related->getKeyName(); + $foreignKey = $model->{$relation['relation']}()->getForeignKeyName(); + $ownerKey = $model->{$relation['relation']}()->getOwnerKeyName(); $query->select($model->getTable() . '.*'); - $query->leftJoin($related->getTable(), $model->qualifyColumn($foreignKey), '=', $related->qualifyColumn($related->getKeyName())); + $query->leftJoin($related->getTable(), $model->qualifyColumn($foreignKey), '=', $related->qualifyColumn($ownerKey)); if (is_string($relation['columns'])) { $qualified = $related->qualifyColumn($relation['columns']); $query->orderBy($qualified, $direction); } if (is_array($relation['columns'])) { foreach ($relation['columns'] as $orderColumn) { - $query->orderBy($related->qualifyColumn($orderColumn), $direction); + $qualified = $related->qualifyColumn($orderColumn); + $query->orderBy($qualified, $direction); } } From 66755b81767c4909d43de52cf55196b791164ae7 Mon Sep 17 00:00:00 2001 From: Newton Evangelista da Gama Junior Date: Sun, 17 May 2020 18:49:35 -0300 Subject: [PATCH 5/8] Readme adjusted to new modifications. --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b64c33b..9b8c8a9 100644 --- a/README.md +++ b/README.md @@ -24,21 +24,21 @@ use LifeOnScreen\SortRelations\SortRelations; class Product extends Resource { public static $sortRelations = [ - // overriding id with product.id (this prevent ambiguous id, if you select multiple ids) - 'id' => 'product.id', + // Order product relation by product id... + 'product' => 'id', // overriding user relation sorting 'user' => [ // sorting multiple columns - 'users.name', - 'users.surname', + 'name', + 'surname', ], // overriding company relation sorting - 'company' => 'company.name', + 'company' => 'name', ]; public static function indexQuery(NovaRequest $request, $query) { - // You can modify your base query here. + // You can modify your base query here, only if necessary. Sort Relations will be applied automatically... return $query; } } @@ -62,4 +62,4 @@ MIT license. Please see the [license file](docs/license.md) for more information [link-packagist]: https://packagist.org/packages/lifeonscreen/nova-sort-relations [link-downloads]: https://packagist.org/packages/lifeonscreen/nova-sort-relations -[link-author]: https://github.com/LifeOnScreen \ No newline at end of file +[link-author]: https://github.com/LifeOnScreen From 5393d59ff5f45a6d01b97c2fd59602eac5d2a0fa Mon Sep 17 00:00:00 2001 From: Newton Evangelista da Gama Junior Date: Sun, 17 May 2020 18:51:26 -0300 Subject: [PATCH 6/8] Readme adjustments --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 9b8c8a9..87b95e5 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,7 @@ $ composer require lifeonscreen/nova-sort-relations ## Usage -Include `LifeOnScreen\SortRelations\SortRelations` trait to your class. Define base by overriding `indexQuery`. -Define sortable columns in `$sortRelations` array. +Include `LifeOnScreen\SortRelations\SortRelations` trait to your class. Define sortable columns in `$sortRelations` array. ```php From 98361baa0faf9c252229d63811eb7d8185312531 Mon Sep 17 00:00:00 2001 From: Newton Evangelista da Gama Junior Date: Sun, 17 May 2020 21:04:17 -0300 Subject: [PATCH 7/8] Code review --- src/SortRelations.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SortRelations.php b/src/SortRelations.php index a65746e..841ebab 100644 --- a/src/SortRelations.php +++ b/src/SortRelations.php @@ -20,9 +20,9 @@ public static function sortableRelations($query): array $model = $query->getModel(); $return = []; if (static::$sortRelations) { - foreach (static::$sortRelation as $relation => $columns) { + foreach (static::$sortRelations as $relation => $columns) { $relatedKey = $model->{$relation}()->getForeignKeyName(); - $return[$relatedkey] = ['relation' => $relation, 'columns' => $columns]; + $return[$relatedKey] = ['relation' => $relation, 'columns' => $columns]; } } return $return; From 98e54a83e8b751364090ae43fc5deab5109cf508 Mon Sep 17 00:00:00 2001 From: Newton Evangelista da Gama Junior Date: Sun, 17 May 2020 23:17:51 -0300 Subject: [PATCH 8/8] Include database connection. Database connection informed on leftJoin to resolve problems for tables residing on other databases. --- src/SortRelations.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SortRelations.php b/src/SortRelations.php index 841ebab..b715d95 100644 --- a/src/SortRelations.php +++ b/src/SortRelations.php @@ -48,7 +48,7 @@ protected static function applyRelationOrderings(string $column, string $directi $ownerKey = $model->{$relation['relation']}()->getOwnerKeyName(); $query->select($model->getTable() . '.*'); - $query->leftJoin($related->getTable(), $model->qualifyColumn($foreignKey), '=', $related->qualifyColumn($ownerKey)); + $query->leftJoin($related->getConnection()->getDatabaseName() . '.' . $related->getTable(), $model->qualifyColumn($foreignKey), '=', $related->qualifyColumn($ownerKey)); if (is_string($relation['columns'])) { $qualified = $related->qualifyColumn($relation['columns']); $query->orderBy($qualified, $direction);