diff --git a/src/Fields/Relation.php b/src/Fields/Relation.php index 9c5fe811..ef42c715 100644 --- a/src/Fields/Relation.php +++ b/src/Fields/Relation.php @@ -201,7 +201,7 @@ public function isNullable(): bool } /** - * Set the searachable attribute. + * {@inheritdoc} */ public function searchable(bool|Closure $value = true, array $columns = ['id']): static { @@ -218,6 +218,18 @@ public function getSearchableColumns(): array return $this->searchableColumns; } + /** + * {@inheritdoc} + */ + public function isSearchable(): bool + { + if ($this->isSubResource()) { + return false; + } + + return parent::isSearchable(); + } + /** * Set the sortable attribute. */ @@ -236,6 +248,18 @@ public function getSortableColumn(): string return $this->sortableColumn; } + /** + * {@inheritdoc} + */ + public function isSortable(): bool + { + if ($this->isSubResource()) { + return false; + } + + return parent::isSortable(); + } + /** * Set the display resolver. */ diff --git a/src/Fields/URL.php b/src/Fields/URL.php new file mode 100644 index 00000000..7bf0d09f --- /dev/null +++ b/src/Fields/URL.php @@ -0,0 +1,18 @@ +type('url'); + } +} diff --git a/src/Filters/Sort.php b/src/Filters/Sort.php index f895fd68..cad742ba 100644 --- a/src/Filters/Sort.php +++ b/src/Filters/Sort.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\Relation as EloquentRelation; +use Illuminate\Database\Query\Expression; use Illuminate\Http\Request; class Sort extends Filter @@ -53,11 +54,12 @@ public function apply(Request $request, Builder $query, mixed $value): Builder $relation = EloquentRelation::noConstraints(static function () use ($query, $value): EloquentRelation { $relation = call_user_func([$query->getModel(), $value['by']]); - $key = $relation instanceof BelongsTo - ? $relation->getQualifiedOwnerKeyName() - : $relation->getQualifiedParentKeyName(); + $key = match (true) { + $relation instanceof BelongsTo => $relation->getQualifiedOwnerKeyName(), + default => $relation->getQualifiedParentKeyName(), + }; - return $relation->whereField($relation->getQualifiedForeignKeyName(), '=', $key); + return $relation->whereColumn($relation->getQualifiedForeignKeyName(), $key); }); return $query->orderBy(