Code Monkey home page Code Monkey logo

larasort's Introduction

Larasort

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

alt text alt text

Installation

Add this package name in composer.json

"require": {
  "monaye/larasort": "2.*"
}

Execute composer command.

composer update

Register the service provider in app.php

'providers' => [
    ...Others...,  
    Monaye\Larasort\LarasortServiceProvider::class,
]

Also alias

'aliases' => [
    ...Others...,  
    'Larasort'   => Monaye\Larasort\Facades\Larasort::class
]

Usage

Minimal way

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

(example)
alt text

with Options

echo \Larasort::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.

\Larasort::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 = \Larasort::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 Monaye Kuhoh

larasort's People

Contributors

sukohi avatar unilance avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.