Skip to content

Commit 5936c15

Browse files
authored
Merge pull request #8 from ghonijee/development
Update Feature SortBy Relation data
2 parents bdd0ec4 + 2b586aa commit 5936c15

File tree

8 files changed

+98
-16
lines changed

8 files changed

+98
-16
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ All notable changes to `QueryAdapter` will be documented in this file
1313

1414
## 1.0.4-Beta - 2021-11-03
1515

16-
- Fixing Bug Pagination Request
16+
- Fixing Bug Pagination Request
17+
18+
## 1.0.5-Beta - 2021-11-04
19+
20+
- Bug Fixed sorted by relation data

Models/TestComment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ class TestComment extends Model
1313

1414
public function test()
1515
{
16-
return $this->belongsTo(TestModel::class);
16+
return $this->belongsTo(TestModel::class, 'test_model_id', 'id');
1717
}
1818
}

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Laravel Query Adapter
22

3-
[![Latest Version on Packagist](https://img.shields.io/packagist/v/ghonijee/laravel-query-adapter.svg?style=flat-square)](https://packagist.org/packages/ghonijee/query-adapter)
4-
[![Total Downloads](https://img.shields.io/packagist/dt/ghonijee/laravel-query-adapter.svg?style=flat-square)](https://packagist.org/packages/ghonijee/query-adapter)
3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/ghonijee/query-adapter.svg?style=flat-square)](https://packagist.org/packages/ghonijee/query-adapter)
4+
[![Total Downloads](https://img.shields.io/packagist/dt/ghonijee/query-adapter.svg?style=flat-square)](https://packagist.org/packages/ghonijee/query-adapter)
55
[![Actions Status](https://github.com/ghonijee/laravel-query-adapter/actions/workflows/main.yml/badge.svg)](https://github.com/ghonijee/laravel-query-adapter/actions)
66

77
## Installation
@@ -15,10 +15,13 @@ composer require ghonijee/query-adapter
1515
## Usage
1616

1717
```php
18+
// Use Facade
1819
$data = QueryAdapter::for(TestModel::class)->get();
1920
// or
2021
$data = QueryAdapter::load(TestModel::query())->get();
21-
//or
22+
23+
24+
//or Use Class DxAdapter
2225
$data = DxAdapter::for(TestModel::class)->get();
2326
// or
2427
$data = DxAdapter::load(TestModel::query())->get();

src/Builders/SortByQuery.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
use GhoniJee\DxAdapter\Data\SortData;
66
use GhoniJee\DxAdapter\SortClass\BuilderSortData;
77
use Illuminate\Database\Eloquent\Builder;
8+
use Illuminate\Database\Eloquent\Relations\Relation;
89
use Illuminate\Support\Collection;
910
use Illuminate\Support\Str;
11+
use ReflectionClass;
12+
use ReflectionMethod;
1013

1114
trait SortByQuery
1215
{
@@ -35,7 +38,33 @@ protected function buildSortQuery()
3538
$this->sort = BuilderSortData::fromRequest($this->sort);
3639

3740
$this->sort->each(function (SortData $item) {
38-
$this->query->orderBy($item->field, $item->type);
41+
if ($item->isRelation) {
42+
if (!is_a($this->query->getModel()->{$item->relationMethod}(), Relation::class)) {
43+
return true;
44+
}
45+
// Get Model instance
46+
$model = $this->query->getModel();
47+
48+
// Get Relation Instance
49+
$relation = $model->{$item->relationMethod}();
50+
51+
// Get TableName of Model Class
52+
$tableNameFrom = $model->getTable();
53+
54+
// Get TableName of Relation Model
55+
$tableRelation = $relation->getRelated()->getTable();
56+
57+
// Get Instance of Relation Target Model
58+
$relationModel = $relation->getRelated();
59+
60+
// Add Order Query with SubQuery
61+
$this->query->orderBy(
62+
$relationModel::select($item->field)
63+
->whereColumn($tableRelation . '.' . $relation->getOwnerKeyName(), $tableNameFrom . '.' . $relation->getForeignKeyName()) // targetTable , FromTable
64+
);
65+
} else {
66+
$this->query->orderBy($item->field, $item->type);
67+
}
3968
});
4069
}
4170
}

src/Data/SortData.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace GhoniJee\DxAdapter\Data;
44

5+
use Illuminate\Support\Collection;
56
use Illuminate\Support\Str;
67

78
class SortData
@@ -10,14 +11,34 @@ class SortData
1011

1112
public $type;
1213

14+
public $relationMethod;
15+
16+
public bool $isRelation = false;
17+
1318
public function __construct(array $data)
1419
{
1520
$this->field = $data['selector'];
21+
$this->isRelationField();
1622
$this->type = $data['desc'] ? 'DESC' : 'ASC';
1723
}
1824

19-
public static function fromRequest(array $data)
25+
public static function fromRequest($data)
2026
{
27+
$data = (array) $data;
2128
return new self($data);
2229
}
30+
31+
public function isRelationField()
32+
{
33+
if (Str::contains($this->field, '.')) {
34+
$this->isRelation = true;
35+
[$this->relationMethod, $this->field] = collect(explode('.', $this->field))
36+
->pipe(function (Collection $parts) {
37+
return [
38+
$parts->except(count($parts) - 1)->implode('.'),
39+
$parts->last(),
40+
];
41+
});
42+
}
43+
}
2344
}

tests/Feature/FilterTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,9 @@
175175
$query = TestModel::query();
176176

177177
$data = DxAdapter::load($query, $this->request)->get();
178+
$expect = TestModel::where('name', 'like', 'ahmad')->get();
178179

179-
expect($data)->toHaveCount(2);
180+
expect($data)->toEqual($expect);
180181
});
181182

182183
test('can multi filter with conjungtion AND', function () {

tests/Feature/SortByTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use GhoniJee\DxAdapter\DxAdapter;
4+
use GhoniJee\DxAdapter\Models\TestComment;
45
use GhoniJee\DxAdapter\Models\TestModel;
56
use Illuminate\Http\Request;
67

@@ -63,3 +64,26 @@
6364

6465
expect($query)->toEqual($queryExpectation);
6566
});
67+
68+
test('can build query for order by relation fieldname with select', function () {
69+
$sort = ['desc' => true, 'selector' => 'test.name'];
70+
$select = ['test_model_id', 'comment'];
71+
72+
$this->request->replace(['sort' => $sort, 'select' => $select]);
73+
74+
$result = DxAdapter::load(TestComment::query()->with('test'), $this->request)->get();
75+
$expected = TestComment::select(['comment', 'test_model_id'])->with('test')->orderBy(
76+
TestModel::select('name')->whereColumn('test_models.id', 'test_comments.test_model_id')
77+
)->get();
78+
expect($result)->toEqual($expected);
79+
});
80+
81+
test('can build query for order by relation fieldname without select', function () {
82+
$sort = ['desc' => true, 'selector' => 'test.name'];
83+
84+
$this->request->replace(['sort' => $sort]);
85+
86+
$result = DxAdapter::for(TestComment::class, $this->request)->first();
87+
$expected = TestComment::where('id', 4)->first();
88+
expect($result)->toEqual($expected);
89+
});

tests/TestCase.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected function createTestModel()
7575

7676
$data = [
7777
[
78-
'name' => "Ahmad",
78+
'name' => "Jee",
7979
'last_name' => "Yunus",
8080
'address' => "Jl. Pendidikan",
8181
'gender' => "L",
@@ -112,31 +112,31 @@ protected function createTestComment()
112112
$comment = [
113113
[
114114
'test_model_id' => 1,
115-
'comment' => "hahsdhshadas"
115+
'comment' => "ccc"
116116
],
117117
[
118118
'test_model_id' => 3,
119-
'comment' => "hahsdhshadas"
119+
'comment' => "ddd"
120120
],
121121
[
122122
'test_model_id' => 1,
123-
'comment' => "hahsdhshadas"
123+
'comment' => "bbb"
124124
],
125125
[
126126
'test_model_id' => 2,
127-
'comment' => "hahsdhshadas"
127+
'comment' => "aaa"
128128
],
129129
[
130130
'test_model_id' => 1,
131-
'comment' => "hahsdhshadas"
131+
'comment' => "eee"
132132
],
133133
[
134134
'test_model_id' => 2,
135-
'comment' => "hahsdhshadas"
135+
'comment' => "ffff"
136136
],
137137
[
138138
'test_model_id' => 3,
139-
'comment' => "hahsdhshadas"
139+
'comment' => "gggg"
140140
]
141141
];
142142
TestComment::insert($comment);

0 commit comments

Comments
 (0)