Skip to content

Commit b5439c9

Browse files
committed
order by related updates
1 parent 8eefef9 commit b5439c9

File tree

9 files changed

+90
-17
lines changed

9 files changed

+90
-17
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ Paginate the given query by 'limit' request parameter:
139139
$news = $this->news->smartPaginate();
140140
```
141141

142+
Add an "order by" clause to the query:
143+
144+
```php
145+
$news = $this->news->orderBy('title', 'desc')->get();
146+
```
147+
142148
Save a new model and return the instance:
143149

144150
```php
@@ -276,9 +282,11 @@ $this->news->scope($request)->get();
276282
```
277283

278284
```
279-
https://example.com/news?orderBy=email&begin=2019-01-24&end=2019-01-26
285+
https://example.com/news?orderBy=email_desc&begin=2019-01-24&end=2019-01-26
280286
```
281287

288+
**orderBy=email_desc** will order by email in descending order, **orderBy=email** - in ascending
289+
282290
You can also build your own custom scopes. In your repository override **scope()** method:
283291

284292
```php

docs/index.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ Paginate the given query by 'limit' request parameter:
132132
$news = $this->news->smartPaginate();
133133
```
134134

135+
Add an "order by" clause to the query:
136+
137+
```php
138+
$news = $this->news->orderBy('title', 'desc')->get();
139+
```
140+
135141
Save a new model and return the instance:
136142

137143
```php
@@ -269,9 +275,11 @@ $this->news->scope($request)->get();
269275
```
270276

271277
```
272-
https://example.com/news?orderBy=email&begin=2019-01-24&end=2019-01-26
278+
https://example.com/news?orderBy=email_desc&begin=2019-01-24&end=2019-01-26
273279
```
274280

281+
**orderBy=email_desc** will order by email in descending order, **orderBy=email** - in ascending
282+
275283
You can also build your own custom scopes. In your repository override **scope()** method:
276284

277285
```php

src/Contracts/RepositoryInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,13 @@ public function findOrFail($id, $columns = ['*']);
122122
* @return mixed
123123
*/
124124
public function smartPaginate();
125+
126+
/**
127+
* Add an "order by" clause to the query.
128+
*
129+
* @param string $column
130+
* @param string $direction
131+
* @return $this
132+
*/
133+
public function orderBy($column, $direction = 'asc');
125134
}

src/Eloquent/BaseRepository.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,18 @@ public function smartPaginate()
214214

215215
return $this->paginate($limit);
216216
}
217+
218+
/**
219+
* Add an "order by" clause to the query.
220+
*
221+
* @param string $column
222+
* @param string $direction
223+
* @return $this
224+
*/
225+
public function orderBy($column, $direction = 'asc')
226+
{
227+
$this->entity = $this->entity->orderBy($column, $direction);
228+
229+
return $this;
230+
}
217231
}

src/Scopes/Clauses/OrderByScope.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66

77
class OrderByScope extends ScopeAbstract
88
{
9-
public function mappings()
10-
{
11-
return [
12-
// 'id' => 'asc',
13-
];
14-
}
15-
169
public function scope($builder, $value, $scope)
1710
{
18-
$field = $value;
11+
$arr = explode('_', $value);
12+
13+
$orderable = $builder->orderable ?? [];
14+
15+
if (array_pop($arr) == 'desc'
16+
&& in_array($field = implode('_', $arr), $orderable)) {
17+
18+
return $builder->orderBy($field, 'desc');
1919

20-
$value = $this->resolveScopeValue($value);
20+
} elseif (in_array($value, $orderable)) {
2121

22-
return is_null($value)
23-
? $builder->orderBy($field, 'desc')
24-
: $builder->orderBy($field, $value);
22+
return $builder->orderBy($value, 'asc');
23+
}
24+
return $builder;
2525
}
2626
}

tests/Stubs/Model.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
class Model extends BaseModel
99
{
10+
public $orderable = ['id'];
11+
1012
protected $fillable = ['name'];
1113

1214
public function submodels()

tests/Unit/RepositoryTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,22 @@ public function it_executes_model_methods()
354354

355355
$result = $repository->submodels();
356356

357-
$this->assertInstanceOf(BelongsToMany::class, $result);
357+
$this->assertInstanceOf(Repository::class, $result);
358+
}
359+
360+
/** @test */
361+
public function it_orders_by()
362+
{
363+
factory(Model::class, 5)->create();
364+
365+
$repository = new Repository;
366+
367+
$results = $repository->get();
368+
369+
$this->assertEquals(1, $results->first()->id);
370+
371+
$results = $repository->orderBy('id', 'desc')->get();
372+
373+
$this->assertEquals(5, $results->first()->id);
358374
}
359375
}

tests/Unit/Scopes/OrderByTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class OrderByTest extends TestCase
1010
{
1111
/** @test */
12-
public function it_orders_by_desc()
12+
public function it_orders_by_asc()
1313
{
1414
factory(Model::class, 5)->create();
1515

@@ -21,6 +21,22 @@ public function it_orders_by_desc()
2121

2222
$results = $scope->scope(new Model, 'id', '')->get();
2323

24+
$this->assertEquals(1, $results->first()->id);
25+
}
26+
27+
/** @test */
28+
public function it_orders_by_desc()
29+
{
30+
factory(Model::class, 5)->create();
31+
32+
$scope = new OrderByScope;
33+
34+
$results = Model::get();
35+
36+
$this->assertEquals(1, $results->first()->id);
37+
38+
$results = $scope->scope(new Model, 'id_desc', '')->get();
39+
2440
$this->assertEquals(5, $results->first()->id);
2541
}
2642
}

tests/Unit/Scopes/ScopesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function it_scopes_request_by_orderBy()
1515
$request = Request::create(
1616
'/',
1717
'GET',
18-
['orderBy' => 'id']
18+
['orderBy' => 'id_desc']
1919
);
2020

2121
$scopes = new Scopes($request, []);

0 commit comments

Comments
 (0)