Skip to content

Commit 9bfde27

Browse files
committed
first commit
0 parents  commit 9bfde27

23 files changed

+1652
-0
lines changed

.editorconfig

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at https://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false
16+
17+
[*.yml]
18+
indent_size = 2

.gitattributes

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Set the default behavior, in case people don't have core.autocrlf set.
2+
* text eol=lf
3+
4+
# Explicitly declare text files you want to always be normalized and converted
5+
# to native line endings on checkout.
6+
*.c text
7+
*.h text
8+
9+
# Declare files that will always have CRLF line endings on checkout.
10+
*.sln text eol=crlf
11+
12+
# Denote all files that are truly binary and should not be modified.
13+
*.png binary
14+
*.jpg binary
15+
*.otf binary
16+
*.eot binary
17+
*.svg binary
18+
*.ttf binary
19+
*.woff binary
20+
*.woff2 binary
21+
22+
*.css linguist-vendored
23+
*.scss linguist-vendored
24+
*.js linguist-vendored
25+
CHANGELOG.md export-ignore

.github/workflows/tests.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
tests:
7+
8+
name: PHP ${{ matrix.php }}
9+
10+
runs-on: ubuntu-latest
11+
12+
strategy:
13+
matrix:
14+
php: ['7.3', '7.4', '8.0', '8.1']
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v2
19+
20+
- name: Cache composer
21+
uses: actions/cache@v1
22+
with:
23+
path: ~/.composer/cache/files
24+
key: php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
25+
26+
- name: Setup PHP
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: ${{ matrix.php }}
30+
extension-csv: bcmath, ctype, dom, fileinfo, intl, gd, json, mbstring, pdo, pdo_sqlite, openssl, sqlite, xml, zip
31+
coverage: none
32+
33+
- name: Install composer
34+
run: composer install --no-interaction --no-scripts --no-suggest --prefer-source
35+
36+
- name: Execute tests
37+
run: vendor/bin/phpunit

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/.idea
2+
/.history
3+
/.vscode
4+
/tests/databases
5+
/vendor
6+
.DS_Store
7+
.phpunit.result.cache
8+
composer.phar
9+
composer.lock

.scrutinizer.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
filter:
2+
excluded_paths:
3+
- tests/*
4+
5+
checks:
6+
php:
7+
code_rating: true
8+
9+
tools:
10+
external_code_coverage: true
11+
php_analyzer: true
12+
php_changetracking: true
13+
php_code_sniffer:
14+
config:
15+
standard: "PSR2"
16+
php_cpd: true
17+
php_mess_detector: true
18+
php_pdepend: true
19+
sensiolabs_security_checker: true

.styleci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
preset: psr2
2+
3+
enabled:
4+
- concat_with_spaces

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2021 Akaunting
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+222
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
# Sortable behavior package for Laravel
2+
3+
![Downloads](https://img.shields.io/packagist/dt/akaunting/laravel-sortable)
4+
![Tests](https://img.shields.io/github/workflow/status/akaunting/laravel-sortable/Tests?label=tests)
5+
[![StyleCI](https://github.styleci.io/repos/442271942/shield?style=flat&branch=master)](https://styleci.io/repos/442271942)
6+
[![Quality](https://img.shields.io/scrutinizer/quality/g/akaunting/laravel-sortable?label=quality)](https://scrutinizer-ci.com/g/akaunting/laravel-sortable)
7+
[![License](https://img.shields.io/github/license/akaunting/laravel-sortable)](LICENSE.md)
8+
9+
This package allows you to add sortable behavior to `models` and `views`. It ships with a trait where you can set the sortable fields and a blade directive to generate table headers automatically.
10+
11+
## Getting Started
12+
13+
### 1. Install
14+
15+
Run the following command:
16+
17+
```bash
18+
composer require akaunting/laravel-sortable
19+
```
20+
21+
### 2. Publish
22+
23+
Publish configuration
24+
25+
```bash
26+
php artisan vendor:publish --tag=sortable
27+
```
28+
29+
### 3. Configure
30+
31+
You can change the column sorting settings of your app from `config/sortable.php` file
32+
33+
## Usage
34+
35+
All you have to do is use the `Sortable` trait inside your model and define the `$sortable` fields.
36+
37+
```php
38+
use Akaunting\Sortable\Traits\Sortable;
39+
use Illuminate\Database\Eloquent\Model;
40+
41+
class Post extends Model
42+
{
43+
use Sortable;
44+
...
45+
46+
public $sortable = [
47+
'id',
48+
'title',
49+
'author',
50+
'created_at',
51+
];
52+
...
53+
}
54+
```
55+
56+
If you don't define the `$sortable` array, the `Scheme::hasColumn()` function is used which runs an extra database query.
57+
58+
### Scope
59+
60+
The trait adds a `sortable` scope to the model so you can use it just before `paginate`:
61+
62+
```php
63+
public function index()
64+
{
65+
$posts = Post::query()->sortable()->paginate(10);
66+
67+
return view('posts.index')->with(['posts' => $posts]);
68+
}
69+
```
70+
71+
You can set also default sorting field which will be applied when URL is empty.
72+
73+
```php
74+
$posts = $post->sortable(['author'])->paginate(10); // $post->orderBy('posts.author', 'asc')
75+
76+
$posts = $post->sortable(['title'])->paginate(10); // $post->orderBy('posts.title', 'asc')
77+
78+
$posts = $post->sortable(['title' => 'desc'])->paginate(10); // $post->orderBy('posts.title', 'desc')
79+
```
80+
81+
### Blade Directive
82+
83+
There is a also `blade` directive for you to create sortable links in your views:
84+
85+
```blade
86+
@sortablelink('title', trans('general.title'), ['parameter' => 'smile'], ['rel' => 'nofollow'])
87+
```
88+
89+
The *first* parameter is the column in database. The *second* one is displayed inside the anchor tag. The *third* one is an `array()`, and it sets the default (GET) query string. The *fourth* one is also an `array()` for additional anchor-tag attributes. You can use a custom URL as 'href' attribute in the fourth parameter, which will append the query string.
90+
91+
Only the first parameter is required.
92+
93+
Examples:
94+
95+
```blade
96+
@sortablelink('title')
97+
@sortablelink('title', trans('general.title'))
98+
@sortablelink('title', trans('general.title'), ['filter' => 'active, visible'])
99+
@sortablelink('title', trans('general.title'), ['filter' => 'active, visible'], ['class' => 'btn btn-success', 'rel' => 'nofollow', 'href' => route('posts.index')])
100+
```
101+
102+
#### Icon Set
103+
104+
You can use any icon set you want. Just change the `icons.wrapper` from the config file accordingly. By default, it uses Font Awesome.
105+
106+
### Sorting Relationships
107+
108+
The package supports `HasOne` and `BelongsTo` relational sorting:
109+
110+
```php
111+
class Post extends Model
112+
{
113+
use Sortable;
114+
...
115+
116+
protected $fillable = [
117+
'title',
118+
'author_id',
119+
'body',
120+
];
121+
122+
public $sortable = [
123+
'id',
124+
'title',
125+
'author',
126+
'created_at',
127+
'updated_at',
128+
];
129+
130+
/**
131+
* Get the author associated with the post.
132+
*/
133+
public function author()
134+
{
135+
return $this->hasOne(\App\Models\Author::class);
136+
}
137+
...
138+
}
139+
```
140+
141+
And you can use the relation in views:
142+
143+
```blade
144+
// resources/views/posts/index.blade.php
145+
146+
@sortablelink('title', trans('general.title'))
147+
@sortablelink('author.name', trans('general.author'))
148+
```
149+
150+
> **Note**: In case there is a self-referencing model (like comments, categories etc.); parent table will be aliased with `parent_` string.
151+
152+
### Advanced Relation
153+
154+
You can also extend the relation sorting feature by creating a function with `Sortable` suffix. There you're free to write your own queries and apply `orderBy()` manually:
155+
156+
```php
157+
class User extends Model
158+
{
159+
use Sortable;
160+
...
161+
162+
public $sortable = [
163+
'name',
164+
'address',
165+
];
166+
167+
public function addressSortable($query, $direction)
168+
{
169+
return $query->join('user_details', 'users.id', '=', 'user_details.user_id')
170+
->orderBy('address', $direction)
171+
->select('users.*');
172+
}
173+
...
174+
```
175+
176+
The usage in `controller` and `view` remains the same.
177+
178+
### Aliasing
179+
180+
You can declare the `$sortableAs` array in your model and use it to alias (bypass column exists check), and ignore prefixing with table:
181+
182+
```php
183+
public $sortableAs = [
184+
'nick_name',
185+
];
186+
```
187+
188+
In controller
189+
190+
```php
191+
$users = $user->select(['name as nick_name'])->sortable(['nick_name'])->paginate(10);
192+
```
193+
194+
In view
195+
196+
```blade
197+
@sortablelink('nick_name', 'nick')
198+
```
199+
200+
It's very useful when you want to sort results using [`withCount()`](https://laravel.com/docs/eloquent-relationships#counting-related-models).
201+
202+
## Changelog
203+
204+
Please see [Releases](../../releases) for more information what has changed recently.
205+
206+
## Contributing
207+
208+
Pull requests are more than welcome. You must follow the PSR coding standards.
209+
210+
## Security
211+
212+
Please review [our security policy](https://github.com/akaunting/laravel-sortable/security/policy) on how to report security vulnerabilities.
213+
214+
## Credits
215+
216+
- [Denis Duliçi](https://github.com/denisdulici)
217+
- [Martin Kiesel](https://github.com/Kyslik)
218+
- [All Contributors](../../contributors)
219+
220+
## License
221+
222+
The MIT License (MIT). Please see [LICENSE](LICENSE.md) for more information.

SECURITY.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Security Policy
2+
3+
**PLEASE DON'T DISCLOSE SECURITY-RELATED ISSUES PUBLICLY, [SEE BELOW](#reporting-a-vulnerability).**
4+
5+
## Reporting a Vulnerability
6+
7+
If you discover any security related issues, please email [email protected] instead of using the issue tracker.

0 commit comments

Comments
 (0)