File tree Expand file tree Collapse file tree 3 files changed +43
-2
lines changed
Expand file tree Collapse file tree 3 files changed +43
-2
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,17 @@ for the soft delete attribute now defaults to the camel-case version of the mode
2424column ` deleted_at ` previously defaulted to the JSON API field ` deleted-at ` , whereas now it will
2525default to ` deletedAt ` . To continue to use dash-case, set the ` softDeleteField ` property on your adapter.
2626
27+ ## [ 2.2.0] - 2020-09-09
28+
29+ ### Added
30+ - [ #549 ] ( https://github.com/cloudcreativity/laravel-json-api/issues/549 )
31+ Can now add sort methods to an Eloquent adapter if sorting is more complex than just sorting by
32+ a column value.
33+
34+ ### Fixed
35+ - The error translator will now detect if the translated value is identical to the translation
36+ key path, and return ` null ` when it is. This fixes behaviour that changed in Laravel 7.28.
37+
2738## [ 2.1.0] - 2020-09-04
2839
2940### Added
Original file line number Diff line number Diff line change @@ -142,3 +142,25 @@ class Adapter extends AbstractAdapter
142142
143143}
144144```
145+
146+ To implement more complex sorting where you need access to the Eloquent query builder instance,
147+ you can add methods to your adapter. The method name must follow the pattern ` sortBy<Name> ` ,
148+ where ` <Name> ` is the camel cased name of the JSON API sort field.
149+
150+ For example, if you had a JSON API sort field called ` likes ` , you could implement the
151+ ` sortByLikes ` method on your adapter. This will receive the query builder instance and
152+ the direction of the sort (either ` asc ` or ` desc ` ).
153+
154+ ``` php
155+ class Adapter extends AbstractAdapter
156+ {
157+ // ...
158+
159+ protected function sortByLikes($query, $direction): void
160+ {
161+ $query->withCount('likes')
162+ ->orderBy('likes_count', $direction);
163+ }
164+
165+ }
166+ ```
Original file line number Diff line number Diff line change @@ -101,13 +101,21 @@ protected function defaultSort()
101101 /**
102102 * @param Builder $query
103103 * @param SortParameterInterface $param
104+ * @return void
104105 */
105106 protected function sortBy ($ query , SortParameterInterface $ param )
106107 {
108+ $ direction = $ param ->isAscending () ? 'asc ' : 'desc ' ;
109+ $ method = 'sortBy ' . Str::classify ($ param ->getField ());
110+
111+ if (method_exists ($ this , $ method )) {
112+ $ this ->{$ method }($ query , $ direction );
113+ return ;
114+ }
115+
107116 $ column = $ this ->getQualifiedSortColumn ($ query , $ param ->getField ());
108- $ order = $ param ->isAscending () ? 'asc ' : 'desc ' ;
109117
110- $ query ->orderBy ($ column , $ order );
118+ $ query ->orderBy ($ column , $ direction );
111119 }
112120
113121 /**
You can’t perform that action at this time.
0 commit comments