sukohi/caruta

A PHP package mainly developed for Laravel to generate sort link(s).

2.0.1 2015-08-15 22:53 UTC

This package is auto-updated.

Last update: 2024-04-09 04:08:27 UTC


README

A PHP package mainly developed for Laravel to generate sort link(s).
(This is for Laravel 5+. For Laravel 4.2)

alt text alt text

Installation

Add this package name in composer.json

"require": {
  "sukohi/caruta": "2.*"
}

Execute composer command.

composer update

Register the service provider in app.php

'providers' => [
    ...Others...,  
    Sukohi\Caruta\CarutaServiceProvider::class,
]

Also alias

'aliases' => [
    ...Others...,  
    'Caruta'   => Sukohi\Caruta\Facades\Caruta::class
]

Usage

Minimal way

{{ \Caruta::links('your-column-name') }}

(example)
alt text

with Options

echo \Caruta::url('http://example.com')  
    ->text('↑', '↓')  
    ->appends([
		'key1' => 'value1',  
		'key2' => 'value2',  
		'key3' => 'value3'  
	])
	->keys('order', 'direction')
	->links('column_name', $separator = ''); 
  • All methods except links() are optional. See methods

Single Text Way

If you set the third argument like the below, only one link will be displayed.

\Caruta::text(
    '<i class="fa fa-sort-asc"></i>',  
    '<i class="fa fa-sort-desc"></i>',  
    '<i class="fa fa-sort"></i>'
);

(example)

alt text

Sort with model
With model(Eloquent), you can automatically set "ORDER BY" like the below.

$items = \App\Item::select('id', 'title');
$items = \Caruta::sort($items, 
    ['id', 'title', 'created_at'], 
    ['updated_at', 'asc']
);
dd($items->get()->toArray());
  • The second argument(Array) means that except specific column name(s) will be ignored to set "ORDER BY" for secure.
  • The third argument(Array) will be used for default. And direction canbe asc and desc

Note: If you changed the parameter name "ORDER BY" to other using keys() method, you also need to set it in this case as well.

Methods

  • url($url)

$url is base URL that will be included in href property.

  • text($one, $two)

$one and $two are text that will be included in link tag.

e.g. <a href="****">YOUR-TEXT</a>

  • appends($values)

$values is additional values that you want to include in link URL.

e.g. http://example.com?orderby=*****&direction=asc&YOUR-KEY=YOUR-VALUE

License

This package is licensed under the MIT License.

Copyright 2014 Sukohi Kuhoh