Sorts on relationships with Spaties QueryBuilder

Viewed 198

Having the following QueryBuilder statement how can I define sorts on related models columns?

$members = QueryBuilder::for(Member::class)
    ->allowedIncludes('user.name', 'association', 'affiliate')
    ->with('user')
    ->allowedFilters([
        AllowedFilter::exact('association.id'),
        AllowedFilter::partial('zip'),
        AllowedFilter::exact('specialisations.id')
    ])
    ->allowedSorts(['user.name', 'zip'])
    ->where('published', 1);

In this case the model Member is related to User and I want sort by User models name. Declaring the sort case allowedSorts(['user.name']) would not work.

Than I implemented the Sort interface to create a custom sorting on relationship as it follows

    $members = QueryBuilder::for(Member::class)
        ->with('user')
        ->allowedIncludes(['user', 'association', 'affiliate'])
        ->allowedFilters([
            AllowedFilter::exact('association.id'),
            AllowedFilter::partial('zip'),
            AllowedFilter::exact('specialisations.id')
        ])
        ->allowedSorts(
            AllowedSort::custom("user.name", new SortByRelation()),
            'zip'
        )
        ->where('published', 1);

the custom sort looks as it follows

public function __invoke(Builder $query, bool $descending, string $property)
{
    [$relationName, $columnName] = explode(".", $property);

    $relation = $query->getRelation($relationName);

    $subQuery = $relation
        ->getQuery()
        ->select($columnName)
        ->whereColumn($relation->getQualifiedForeignKeyName(), $relation->getQualifiedOwnerKeyName());

    $query->orderBy($subQuery, $descending ? "desc" : "asc");
}

which is working on one sorted column but if I sort both of, just one theme will get the right sorting.

sort=zip,user.name is not working properly

0 Answers
Related