2017 © Pedro Peláez
 

library neatness

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

image

sukohi/neatness

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  • Wednesday, September 21, 2016
  • by Sukohi
  • Repository
  • 1 Watchers
  • 1 Stars
  • 2,247 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 27 Versions
  • 13 % Grown

The README.md

Neatness

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.
(This is for Laravel 5+. For Laravel 4.2), (*1)

Demo, (*2)

Installation

Execute composer command., (*3)

composer require sukohi/neatness:4.*

Preparation

At first, set NeatnessTrait in your Model., (*4)

use Sukohi\Neatness\NeatnessTrait;

class Item extends Eloquent {

    use NeatnessTrait;

}

Secondary, add configuration values also in your Model., (*5)

default: Default key and direction. (Required)
columns: Keys and column names you want to sort. (Required)
symbols: Labels you will be able to use in your View. (Optional)
symbols: Symbols you will be able to use in your View. (Optional)
appends: Keys you want to append to URLs. (Optional), (*6)

protected $neatness = [
    'default' => ['sort_id', 'desc'],
    'columns' => [
        'sort_id' => 'id',
        'sort_title' => 'title',
        'sort_date' => 'created_at'
    ],
    'labels' => [
        'sort_id' => 'ID',
        'sort_title' => 'Title',
        'sort_date' => 'Date'
    ],
    'symbols' => [
        'asc' => '<i class="fa fa-sort-asc"></i>',
        'desc' => '<i class="fa fa-sort-desc"></i>',
        'default' => '<i class="fa fa-sort"></i>'
    ],
    'appends' => ['name']
];

Multiple columns: If you want to sort by multiple columns, you can use delimiter | like so., (*7)

'columns' => [
    'id_n_title' => 'id|title'
],

Query Scope: You also can utilize Query Scopes instead of column name., (*8)

'columns' => [
    'scope_title' => 'scope::sortTitle'
],

in this case, you need to prepare a scope method in your model. (About Query Scopes), (*9)

public function scopeSortTitle($query, $direction) {

    return $query->orderBy('title', $direction);

}

Label: You can use label:: prefix to call a specific method., (*10)

'labels' => [
    'title' => 'label::SortTitle'
],

in this case, you need to prepare a method in your model., (*11)

public function labelSortTitle() {

    return 'Your Title'.

}

Usage

Now you can use a method called neatness., (*12)

(in Controller), (*13)

$items = Item::neatness()->get();

After call neatness(), you can access to sort data through $neatness., (*14)

(in View), (*15)

key: The key name sorting now., (*16)

Key: {{ $neatness->key }}

column: The column name sorting now., (*17)

Column: {{ $neatness->column }}

direction: The Direction sorting now. asc or desc, (*18)

Direction: {{ $neatness->direction }}

urls: URLs to switch sort., (*19)

@foreach($neatness->urls as $key => $url)
    {{ $key }} => {{ $url }}
@endforeach

or 

$neatness->urls->get('title');

all_urls: All URLs to switch sort., (*20)

@foreach($neatness->all_urls as $key => $urls)
    @foreach($urls as $direction => $url)
        {{ $direction }} => {{ $url }}<br>
    @endforeach
@endforeach

or 

$neatness->all_urls->get('title');          // Array
$neatness->all_urls->get('title')['desc']   // URL

labels: Labels you set in your Model., (*21)

@foreach($neatness->labels as $key => $label)
    {{ $key }} => {{ $label }}
@endforeach

or 

$neatness->labels->get('title');

symbols: Symbols plucked with sort state., (*22)

@foreach($neatness->symbols as $key => $symbol)
    {{ $key }} => {{ $symbol }}
@endforeach

or 

$neatness->symbols->get('title');

texts: Texts mainly for link., (*23)

@foreach($neatness->urls as $key => $url)
    <a href="{{ $url }}">{{ $neatness->texts->get($key) }}</a>
@endforeach

or 

$neatness->texts->get('title');

appends: Array values for pagination, (*24)

{{ $items->appends($neatness->appends)->links() }}

Change default column and direction

By this way, you can change default column and direction., (*25)

Item::neatness('title', 'desc')->get();

Relationship

You can use this package with relationship using join()., (*26)

(in Controller), (*27)

$items = Item::join('item_details', 'item_details.item_id', '=', 'items.id')
            ->neatness()
            ->paginate(5);

(in Model), (*28)

protected $neatness = [
    'default' => ['items.id', 'desc'],
    'columns' => [
        'id' => 'items.id',
        'title' => 'items.title',
        'date' => 'items.created_at',
        'address' => 'item_details.address'
    ]
];

License

This package is licensed under the MIT License., (*29)

Copyright 2016 Sukohi Kuhoh, (*30)

The Versions

21/09 2016

4.0.x-dev

4.0.9999999.9999999-dev

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

21/09 2016

dev-master

9999999-dev

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

21/09 2016

4.0.2

4.0.2.0

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

14/07 2016

4.0.1

4.0.1.0

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

25/05 2016

3.0.x-dev

3.0.9999999.9999999-dev

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

25/05 2016

3.0.1

3.0.1.0

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

17/04 2016

4.0.0

4.0.0.0

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

17/04 2016

3.0.0

3.0.0.0

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

15/04 2016

1.0.x-dev

1.0.9999999.9999999-dev

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

15/04 2016

1.0.9

1.0.9.0

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

15/04 2016

2.0.x-dev

2.0.9999999.9999999-dev

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

15/04 2016

2.0.6

2.0.6.0

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

15/04 2016

2.0.5

2.0.5.0

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

15/04 2016

1.0.8

1.0.8.0

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

15/04 2016

2.0.4

2.0.4.0

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

15/04 2016

1.0.7

1.0.7.0

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

15/04 2016

1.0.6

1.0.6.0

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

15/04 2016

1.0.5

1.0.5.0

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

14/04 2016

1.0.4

1.0.4.0

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

14/04 2016

2.0.3

2.0.3.0

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

14/04 2016

1.0.3

1.0.3.0

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

14/04 2016

2.0.2

2.0.2.0

A Laravel package to automatically add sorting system for DB query and provide URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

14/04 2016

2.0.1

2.0.1.0

A Laravel package to automatically add sorting system for DB query and providing URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

14/04 2016

1.0.2

1.0.2.0

A Laravel package to automatically add sorting system for DB query and providing URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

14/04 2016

1.0.1

1.0.1.0

A Laravel package to automatically add sorting system for DB query and providing URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

14/04 2016

2.0.0

2.0.0.0

A Laravel package to automatically add sorting system for DB query and providing URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

14/04 2016

1.0.0

1.0.0.0

A Laravel package to automatically add sorting system for DB query and providing URLs to switch between ASC and DESC.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi