Skip to content

Commit

Permalink
Merge pull request #10 from JimChenWYU/2.x
Browse files Browse the repository at this point in the history
Upgrade 2.x
  • Loading branch information
jcc authored Mar 14, 2021
2 parents a49eedf + 00c2a44 commit d7cc174
Show file tree
Hide file tree
Showing 28 changed files with 1,093 additions and 265 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Lint
on: [ push, pull_request ]

jobs:
lint:
name: PHP-${{ matrix.php_version }}-${{ matrix.perfer }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php_version:
- 7.4
perfer:
- stable
container:
image: nauxliu/php-ci-image:${{ matrix.php_version }}
steps:
- uses: actions/checkout@master
- name: Install Dependencies
run: composer install --prefer-dist --no-interaction --no-suggest
- name: Check Style
run: composer check-style
- name: PHPStan analyse
run: composer phpstan
24 changes: 24 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test
on: [push, pull_request]

jobs:
phpunit:
name: PHP-${{ matrix.php_version }}-${{ matrix.perfer }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php_version:
- 7.2
- 7.3
- 7.4
perfer:
- stable
container:
image: nauxliu/php-ci-image:${{ matrix.php_version }}
steps:
- uses: actions/checkout@master
- name: Install Dependencies
run: composer install --prefer-dist --no-interaction --no-suggest
- name: Run PHPUnit
run: ./vendor/bin/phpunit
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.idea
/vendor/
composer.lock
.phpunit.result.cache
.php_cs.cache
.DS_Store
48 changes: 48 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'binary_operator_spaces' => true,
'blank_line_after_opening_tag' => true,
'compact_nullable_typehint' => true,
'declare_equal_normalize' => true,
'lowercase_cast' => true,
'lowercase_static_reference' => true,
'new_with_braces' => true,
'no_blank_lines_after_class_opening' => true,
'no_leading_import_slash' => true,
'no_whitespace_in_blank_line' => true,
'ordered_class_elements' => [
'order' => [
'use_trait',
],
],
'ordered_imports' => [
'imports_order' => [
'class',
'function',
'const',
],
'sort_algorithm' => 'none',
],
'return_type_declaration' => true,
'short_scalar_cast' => true,
'single_blank_line_before_namespace' => true,
'single_trait_insert_per_statement' => true,
'ternary_operator_spaces' => true,
'unary_operator_spaces' => true,
'visibility_required' => [
'elements' => [
'const',
'method',
'property',
],
],
])
->setFinder(
PhpCsFixer\Finder::create()
->exclude('vendor')
->in([__DIR__.'/src/', __DIR__.'/tests/', __DIR__.'/config/', __DIR__.'/migrations/'])
)
;
97 changes: 76 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# Laravel 5 Vote System
# Laravel Vote System

:tada: This package helps you to add user based vote system to your model.

> This project code is basically the same as [laravel-follow](https://github.com/overtrue/laravel-follow).
## Installation

You can install the package using Composer:

```sh
$ composer require jcc/laravel-vote -vvv
$ composer require "jcc/laravel-vote:~2.0"
```

Then add the service provider to `config/app.php`:
Expand All @@ -27,24 +25,22 @@ $ php artisan vendor:publish --provider="Jcc\LaravelVote\VoteServiceProvider" --
Finally, use VoteTrait in User model:

```php
use Jcc\LaravelVote\Vote;
use Jcc\LaravelVote\Traits\Voter;

class User extends Model
{
use Vote;
use Voter;
}
```

Or use CanBeVoted in Comment model:

```php
use Jcc\LaravelVote\CanBeVoted;
use Jcc\LaravelVote\Traits\Votable;

class Comment extends Model
{
use CanBeVoted;

protected $vote = User::class;
use Votable;
}
```

Expand Down Expand Up @@ -79,28 +75,28 @@ $user->cancelVote($comment);
#### Get user has voted comment items

```php
$user->votedItems(Comment::class)->get();
$user->getVotedItems(Comment::class)->get();
```

#### Check if user has up or down vote

```
```php
$comment = Comment::find(1);

$user->hasVoted($comment);
```

#### Check if user has up vote

```
```php
$comment = Comment::find(1);

$user->hasUpVoted($comment);
```

#### Check if user has down vote

```
```php
$comment = Comment::find(1);

$user->hasDownVoted($comment);
Expand All @@ -111,37 +107,96 @@ $user->hasDownVoted($comment);
#### Get comment voters

```php
$comment->voters();
$comment->voters()->get();
```

#### Count comment voters

```php
$comment->countVoters();
$comment->voters()->count();
```

#### Get comment up voters

```php
$comment->upVoters()->get();
```

#### Count comment up voters

```php
$comment->countUpVoters();
$comment->upVoters()->count();
```

#### Get comment down voters

```php
$comment->downVoters()->get();
```

#### Count comment down voters

```php
$comment->countDownVoters();
$comment->downVoters()->count();
```

#### Check if voted by

```php
$comment->isVotedBy(1);
$user = User::find(1);

$comment->isVotedBy($user);
```

#### Check if up voted by

```php
$user = User::find(1);

$comment->isUpVotedBy($user);
```

#### Check if down voted by

```php
$user = User::find(1);

$comment->isDownVotedBy($user);
```

### N+1 issue

To avoid the N+1 issue, you can use eager loading to reduce this operation to just 2 queries. When querying, you may specify which relationships should be eager loaded using the `with` method:

```php
// Voter
$users = User::with('votes')->get();

foreach($users as $user) {
$user->hasVoted($comment);
}

// Votable
$comments = Comment::with('voters')->get();

foreach($comments as $comment) {
$comment->isVotedBy($user);
}
```

### Events

| **Event** | **Description** |
| --- | --- |
| `Jcc\LaravelVote\Events\Voted` | Triggered when the relationship is created or updated. |
| `Jcc\LaravelVote\Events\CancelVoted` | Triggered when the relationship is deleted. |


## Reference

[laravel-follow](https://github.com/overtrue/laravel-follow)
- [laravel-follow](https://github.com/overtrue/laravel-follow)
- [laravel-like](https://github.com/overtrue/laravel-like)

## License

MIT
[MIT](LICENSE)
19 changes: 15 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
}
],
"require": {
"php": ">=5.5.9"
"php": ">=7.2",
"laravel/framework": "^5.5|~6.0|~7.0|~8.0",
"symfony/polyfill-php80": "^1.22"
},
"require-dev": {
"phpunit/phpunit": "~5.0"
"mockery/mockery": "^1.3",
"orchestra/testbench": "^3.5|~4.0|~5.0|~6.0",
"friendsofphp/php-cs-fixer": "^2.18",
"phpstan/phpstan": "^0.12.81"
},
"license": "MIT",
"autoload": {
Expand All @@ -31,6 +36,12 @@
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
"minimum-stability": "stable",
"prefer-stable": true,
"scripts": {
"check-style": "vendor/bin/php-cs-fixer fix --using-cache=no --diff --config=.php_cs --dry-run --ansi",
"fix-style": "vendor/bin/php-cs-fixer fix --using-cache=no --config=.php_cs --ansi",
"test": "vendor/bin/phpunit --colors=always",
"phpstan": "vendor/bin/phpstan analyse src -l 5"
}
}
10 changes: 10 additions & 0 deletions config/vote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [

'votes_table' => 'votes',

'user_foreign_key' => 'user_id',

'vote_model' => \Jcc\LaravelVote\Vote::class,
];
34 changes: 0 additions & 34 deletions database/migrations/create_votes_table.php

This file was deleted.

Loading

0 comments on commit d7cc174

Please sign in to comment.