Skip to content
This repository was archived by the owner on Mar 21, 2021. It is now read-only.

Commit a5e44d8

Browse files
author
Pe Ell
authored
Sort likeable models by likes count (#15)
* Add scope to fetch likeable models by likes count
1 parent 542d523 commit a5e44d8

12 files changed

+292
-57
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
/.gitattributes export-ignore
55
/.gitignore export-ignore
66
/.php_cs export-ignore
7+
/.styleci.yml export-ignore
8+
/.travis.yml export-ignore
79
/CODE_OF_CONDUCT.md export-ignore
810
/CONTRIBUTING.md export-ignore
911
/phpunit.xml.dist export-ignore

.travis.yml

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
language: php
22

3+
sudo: false
4+
35
php:
46
- 5.6
57
- 7.0
68
- 7.1
79

10+
env:
11+
global:
12+
- setup=basic
13+
814
before_script:
9-
- travis_retry composer self-update
10-
- travis_retry composer install --no-interaction
15+
- composer config discard-changes true
16+
- if [[ $setup = 'basic' ]]; then travis_retry composer install --prefer-dist --no-interaction; fi
17+
- if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-stable; fi
18+
- if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-lowest --prefer-stable; fi
1119

12-
script: vendor/bin/phpunit --verbose
20+
script:
21+
- ./vendor/bin/phpunit -c phpunit.xml.dist --verbose

CHANGELOG.md

+13-6
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,40 @@
22

33
All notable changes to `laravel-likeable` will be documented in this file.
44

5-
## 2.1.0 - 2017-02-20
5+
## [2.2.0] - 2017-04-09
6+
7+
### Added
8+
9+
- `Article::sortedByLikesCount('asc')` scope for the model. Uses `desc` as default order direction.
10+
11+
## [2.1.0] - 2017-02-20
612

713
### Added
814

915
- Laravel 5.4 support.
1016

11-
## 2.0.1 - 2017-01-11
17+
## [2.0.1] - 2017-01-11
1218

1319
- Removed unused properties in `LikeObserver` #12
1420
- Foreign key in migration commented out #11
1521

16-
## 2.0.0 - 2016-09-11
22+
## [2.0.0] - 2016-09-11
1723

1824
- Renamed `FollowableService` methods to follow code style consistency:
1925
- `incrementLikeCount()` to `incrementLikesCount()`
2026
- `decrementLikeCount()` to `decrementLikesCount()`
2127
- `decrementDislikeCount()` to `decrementDislikesCount()`
2228
- `incrementDislikeCount()` to `incrementDislikesCount()`
2329

24-
## 1.1.2 - 2016-09-07
30+
## [1.1.2] - 2016-09-07
2531

2632
- Fix enum like types
2733

28-
## 1.1.1 - 2016-09-07
34+
## [1.1.1] - 2016-09-07
2935

3036
- Fix likeable enums database default value
3137

32-
## 1.1.0 - 2016-09-07
38+
## [1.1.0] - 2016-09-07
3339

3440
- Renamed `HasLikes` trait methods to follow code style consistency:
3541
- `likeCounter()` to `likesCounter()`
@@ -39,6 +45,7 @@ All notable changes to `laravel-likeable` will be documented in this file.
3945

4046
- Initial release
4147

48+
[2.2.0]: https://github.com/cybercog/laravel-likeable/compare/2.1.0...2.2.0
4249
[2.1.0]: https://github.com/cybercog/laravel-likeable/compare/2.0.1...2.1.0
4350
[2.0.1]: https://github.com/cybercog/laravel-likeable/compare/2.0.0...2.0.1
4451
[2.0.0]: https://github.com/cybercog/laravel-likeable/compare/1.1.2...2.0.0

CONTRIBUTING.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ Due to time constraints, we are not always able to respond as quickly as we woul
2121

2222
This project comes with a configuration file for php-cs-fixer (.php_cs) that you can use to (re)format your sourcecode for compliance with this project's coding guidelines:
2323

24-
```shell
24+
```sh
2525
vendor/bin/php-cs-fixer fix
2626
```
2727

2828
## PHPUnit tests
2929

3030
The phpunit script can be used to invoke the PHPUnit test runner:
3131

32-
```shell
32+
```sh
3333
vendor/bin/phpunit
3434
```
3535

README.md

+54-22
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,27 @@
99

1010
## Introduction
1111

12-
Trait for Laravel Eloquent models to allow easy implementation of a `like` & `dislike` features.
12+
Laravel Likeable simplify management of Eloquent model's likes & dislikes. Make any model `likeable` & `dislikeable` in a minutes!
13+
14+
## Contents
15+
16+
- [Features](#features)
17+
- [Installation](#installation)
18+
- [Usage](#usage)
19+
- [Prepare likeable model](#prepare-likeable-model)
20+
- [Available methods](#available-methods)
21+
- [Scopes](#scopes)
22+
- [Events](#events)
23+
- [Console commands](#console-commands)
24+
- [Extending](#extending)
25+
- [Change log](#change-log)
26+
- [Contributing](#contributing)
27+
- [Testing](#testing)
28+
- [Security](#security)
29+
- [Credits](#credits)
30+
- [Alternatives](#alternatives)
31+
- [License](#license)
32+
- [About CyberCog](#about-cybercog)
1333

1434
## Features
1535

@@ -20,14 +40,15 @@ Trait for Laravel Eloquent models to allow easy implementation of a `like` & `di
2040
- Has Artisan command `likeable:recount {model?} {type?}` to re-fetch likes counters.
2141
- Likeable model can has Likes and Dislikes.
2242
- Likes and Dislikes for one model are mutually exclusive.
43+
- Get Likeable models ordered by likes count.
2344
- Events for `like`, `unlike`, `dislike`, `undislike` methods.
2445
- Covered with unit tests.
2546

2647
## Installation
2748

2849
First, pull in the package through Composer.
2950

30-
```shell
51+
```sh
3152
composer require cybercog/laravel-likeable
3253
```
3354

@@ -41,7 +62,7 @@ And then include the service provider within `app/config/app.php`.
4162

4263
At last you need to publish and run database migrations.
4364

44-
```shell
65+
```sh
4566
php artisan vendor:publish --provider="Cog\Likeable\Providers\LikeableServiceProvider" --tag=migrations
4667
php artisan migrate
4768
```
@@ -62,7 +83,7 @@ class Article extends Model implements HasLikesContract {
6283
}
6384
```
6485

65-
### Available functions
86+
### Available methods
6687

6788
#### Likes
6889

@@ -222,6 +243,17 @@ $article->likesAndDislikes();
222243
$article->likesAndDislikes;
223244
```
224245

246+
### Scopes
247+
248+
##### Fetch Likeable models by likes count
249+
250+
```php
251+
$sortedArticles = Article::sortedByLikesCount()->get();
252+
$sortedArticles = Article::sortedByLikesCount('asc')->get();
253+
```
254+
255+
*Uses `desc` as default order direction.*
256+
225257
### Events
226258

227259
On each like added `\Cog\Likeable\Events\ModelWasLiked` event is fired.
@@ -236,49 +268,49 @@ On each dislike removed `\Cog\Likeable\Events\ModelWasUndisliked` event is fired
236268

237269
##### Recount likes and dislikes of all model types
238270

239-
```shell
271+
```sh
240272
likeable:recount
241273
```
242274

243275
##### Recount likes and dislikes of concrete model type (using morph map alias)
244276

245-
```shell
277+
```sh
246278
likeable:recount --model="article"
247279
```
248280

249281
##### Recount likes and dislikes of concrete model type (using fully qualified class name)
250282

251-
```shell
283+
```sh
252284
likeable:recount --model="App\Models\Article"
253285
```
254286

255287
##### Recount only likes of all model types
256288

257-
```shell
289+
```sh
258290
likeable:recount --type="like"
259291
```
260292

261293
##### Recount only likes of concrete model type (using morph map alias)
262294

263-
```shell
295+
```sh
264296
likeable:recount --model="article" --type="like"
265297
```
266298

267299
##### Recount only likes of concrete model type (using fully qualified class name)
268300

269-
```shell
301+
```sh
270302
likeable:recount --model="App\Models\Article" --type="like"
271303
```
272304

273305
##### Recount only dislikes of all model types
274306

275-
```shell
307+
```sh
276308
likeable:recount --type="dislike"
277309
```
278310

279311
##### Recount only dislikes of concrete model type (using morph map alias)
280312

281-
```shell
313+
```sh
282314
likeable:recount --model="article" --type="dislike"
283315
```
284316

@@ -325,31 +357,31 @@ $model = app(\Cog\Likeable\Contracts\Like::class);
325357
$service = app(\Cog\Likeable\Contracts\LikeableService::class);
326358
```
327359

360+
## Change log
361+
362+
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
363+
364+
## Contributing
365+
366+
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
367+
328368
## Testing
329369

330370
You can run the tests with:
331371

332-
```shell
372+
```sh
333373
vendor/bin/phpunit
334374
```
335375

336-
## Contributing
337-
338-
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
339-
340376
## Security
341377

342-
If you discover any security related issues, please email a.komarev@cybercog.su instead of using the issue tracker.
378+
If you discover any security related issues, please email oss@cybercog.su instead of using the issue tracker.
343379

344380
## Credits
345381

346382
- [Anton Komarev](https://github.com/a-komarev)
347383
- [All Contributors](../../contributors)
348384

349-
## Changelog
350-
351-
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
352-
353385
## Alternatives
354386

355387
- [rtconner/laravel-likeable](https://github.com/rtconner/laravel-likeable)

composer.json

+10-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
"type": "library",
55
"license": "MIT",
66
"keywords": [
7-
"trait",
7+
"cybercog",
8+
"cog",
89
"laravel",
910
"eloquent",
11+
"trait",
1012
"likeable",
1113
"likable",
1214
"like",
1315
"dislike",
1416
"favorite",
1517
"favourite",
1618
"rate",
17-
"rating"
19+
"rating",
20+
"emotion"
1821
],
1922
"authors": [
2023
{
@@ -26,7 +29,7 @@
2629
],
2730
"homepage": "https://github.com/cybercog/laravel-likeable",
2831
"support": {
29-
"email": "a.komarev@cybercog.su",
32+
"email": "oss@cybercog.su",
3033
"issues": "https://github.com/cybercog/laravel-likeable/issues",
3134
"wiki": "https://github.com/cybercog/laravel-likeable/wiki",
3235
"source": "https://github.com/cybercog/laravel-likeable",
@@ -39,7 +42,7 @@
3942
},
4043
"require-dev": {
4144
"friendsofphp/php-cs-fixer": "^1.11",
42-
"mockery/mockery": "^0.9.5",
45+
"mockery/mockery": "^0.9.8",
4346
"orchestra/database": "~3.4.0",
4447
"orchestra/testbench": "~3.4.0",
4548
"phpunit/phpunit": "^5.7"
@@ -54,6 +57,9 @@
5457
"Cog\\Likeable\\Tests\\": "tests/"
5558
}
5659
},
60+
"scripts": {
61+
"test": "vendor/bin/phpunit"
62+
},
5763
"config": {
5864
"sort-packages": true
5965
},

src/Contracts/HasLikes.php

+17-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Cog\Likeable\Contracts;
1313

14+
use Illuminate\Database\Eloquent\Builder;
15+
1416
/**
1517
* Interface HasLikes.
1618
*
@@ -91,24 +93,33 @@ public function getLikesDiffDislikesCountAttribute();
9193
/**
9294
* Fetch records that are liked by a given user id.
9395
*
94-
* @param \Illuminate\Database\Query\Builder $query
96+
* @param \Illuminate\Database\Eloquent\Builder $query
9597
* @param int|null $userId
96-
* @return \Illuminate\Database\Query\Builder
98+
* @return \Illuminate\Database\Eloquent\Builder
9799
*
98100
* @throws \Cog\Likeable\Exceptions\LikerNotDefinedException
99101
*/
100-
public function scopeWhereLikedBy($query, $userId = null);
102+
public function scopeWhereLikedBy(Builder $query, $userId = null);
101103

102104
/**
103105
* Fetch records that are disliked by a given user id.
104106
*
105-
* @param \Illuminate\Database\Query\Builder $query
107+
* @param \Illuminate\Database\Eloquent\Builder $query
106108
* @param int|null $userId
107-
* @return \Illuminate\Database\Query\Builder
109+
* @return \Illuminate\Database\Eloquent\Builder
108110
*
109111
* @throws \Cog\Likeable\Exceptions\LikerNotDefinedException
110112
*/
111-
public function scopeWhereDislikedBy($query, $userId = null);
113+
public function scopeWhereDislikedBy(Builder $query, $userId = null);
114+
115+
/**
116+
* Fetch records sorted by likes count.
117+
*
118+
* @param \Illuminate\Database\Eloquent\Builder $query
119+
* @param string $sortDirection
120+
* @return \Illuminate\Database\Eloquent\Builder
121+
*/
122+
public function scopeSortedByLikesCount(Builder $query, $sortDirection = 'desc');
112123

113124
/**
114125
* Add a like for model by the given user.

0 commit comments

Comments
 (0)