Skip to content

Commit d981723

Browse files
committed
Added the ability to pass raw values for the order by functions
1 parent 9f622c5 commit d981723

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,12 @@ You can also sort your query results using a column from another table using the
258258
User::orderByPowerJoins('profile.city');
259259
```
260260

261+
If you need to pass some raw values for the order by function, you can do like this:
262+
263+
```php
264+
User::orderByPowerJoins(['profile', DB::raw('concat(city, ", ", state)']);
265+
```
266+
261267
This query will sort the results based on the `city` column on the `user_profiles` table. You can also sort your results by aggregations (`COUNT`, `SUM`, `AVG`, `MIN` or `MAX`).
262268

263269
For instance, to sort users with the highest number of posts, you can do this:

src/PowerJoins.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
namespace Kirschbaum\PowerJoins;
44

55
use Closure;
6+
use Illuminate\Support\Str;
67
use Illuminate\Database\Eloquent\Builder;
8+
use Illuminate\Database\Query\Expression;
79
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
810
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
9-
use Illuminate\Support\Str;
1011

1112
trait PowerJoins
1213
{
@@ -165,9 +166,15 @@ public function scopeJoinNestedRelationship(Builder $query, $relations, $callbac
165166
*/
166167
public function scopeOrderByPowerJoins(Builder $query, $sort, $direction = 'asc', $aggregation = null, $joinType = 'join'): void
167168
{
168-
$relationships = explode('.', $sort);
169-
$column = array_pop($relationships);
170-
$latestRelationshipName = $relationships[count($relationships) - 1];
169+
if (is_array($sort)) {
170+
$relationships = explode('.', $sort[0]);
171+
$column = $sort[1];
172+
$latestRelationshipName = $relationships[count($relationships) - 1];
173+
} else {
174+
$relationships = explode('.', $sort);
175+
$column = array_pop($relationships);
176+
$latestRelationshipName = $relationships[count($relationships) - 1];
177+
}
171178

172179
$query->joinRelationship(implode('.', $relationships), null, $joinType);
173180

@@ -188,7 +195,15 @@ public function scopeOrderByPowerJoins(Builder $query, $sort, $direction = 'asc'
188195
->groupBy(sprintf('%s.%s', $this->getModel()->getTable(), $this->getModel()->getKeyName()))
189196
->orderBy(sprintf('%s_aggregation', $latestRelationshipName), $direction);
190197
} else {
191-
$query->orderBy(sprintf('%s.%s', $latestRelationshipModel->getTable(), $column), $direction);
198+
if ($column instanceof Expression) {
199+
// dd($column);
200+
$query->orderBy($column, $direction);
201+
} else {
202+
$query->orderBy(
203+
sprintf('%s.%s', $latestRelationshipModel->getTable(), $column),
204+
$direction
205+
);
206+
}
192207
}
193208
}
194209

tests/OrderByTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Kirschbaum\PowerJoins\Tests;
44

5-
use Kirschbaum\PowerJoins\Tests\Models\Comment;
5+
use Illuminate\Support\Facades\DB;
66
use Kirschbaum\PowerJoins\Tests\Models\Post;
77
use Kirschbaum\PowerJoins\Tests\Models\User;
8+
use Kirschbaum\PowerJoins\Tests\Models\Comment;
89
use Kirschbaum\PowerJoins\Tests\Models\UserProfile;
910

1011
class OrderByTest extends TestCase
@@ -24,7 +25,7 @@ public function test_order_by_relationship()
2425
$users = User::with('profile')->orderByPowerJoins('profile.city')->get();
2526

2627
// making sure left join do not throw exceptions
27-
User::with('profile')->OrderByLeftPowerJoins('profile.city')->get();
28+
User::with('profile')->orderByLeftPowerJoins('profile.city')->get();
2829

2930
$this->assertEquals('Atlanta', $users->get(0)->profile->city);
3031
$this->assertEquals('Los Angeles', $users->get(1)->profile->city);
@@ -33,6 +34,21 @@ public function test_order_by_relationship()
3334
$this->assertEquals('Veneza', $users->get(4)->profile->city);
3435
}
3536

37+
/** @test */
38+
public function test_order_by_relationship_with_concat()
39+
{
40+
User::with('profile')
41+
->select('user_profiles.*', DB::raw('printf("%s, %s", user_profiles.city, user_profiles.state) as locale'))
42+
->orderByPowerJoins(['profile', DB::raw('locale')])
43+
->get();
44+
45+
User::with('profile')
46+
->orderByPowerJoins(['profile', DB::raw('printf("%s, %s", user_profiles.city, user_profiles.state)')])
47+
->get();
48+
49+
$this->expectNotToPerformAssertions();
50+
}
51+
3652
/** @test */
3753
public function test_can_call_twice_in_a_row()
3854
{

tests/database/migrations/2020_03_16_000000_create_tables.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function up()
2525
$table->increments('id');
2626
$table->unsignedInteger('user_id');
2727
$table->string('city')->nullable();
28+
$table->string('state')->nullable();
2829
$table->timestamps();
2930
});
3031

0 commit comments

Comments
 (0)