-
-
Save stevebauman/adb4a7103c1b800867f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Allows all columns on the current database table to be sorted through | |
* query scope | |
* | |
* @param type $query | |
* @param type $field | |
* @param type $sort | |
* @return object | |
*/ | |
public function scopeSort($query, $field = NULL, $sort = NULL){ | |
/* | |
* Make sure both the field and sort variables are present | |
*/ | |
if($field && $sort){ | |
/* | |
* Retrieve all column names for the current model table | |
*/ | |
$columns = Schema::getColumnListing($this->table); | |
/* | |
* Make sure the field inputted is available on the current table | |
*/ | |
if(in_array($field, $columns)){ | |
/* | |
* Make sure the sort input is equal to asc or desc | |
*/ | |
if($sort === 'asc' || $sort === 'desc'){ | |
/* | |
* Return the query sorted | |
*/ | |
return $query->orderBy($field, $sort); | |
} | |
} | |
} else{ | |
/* | |
* Default order by created at field | |
*/ | |
return $query->orderBy('created_at', 'desc'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$field = Input::get('field'); | |
$sort = Input::get('sort'); | |
if($sort == 'desc'){ | |
$parameters['sort'] = 'asc'; | |
} else{ | |
$parameters['sort'] = 'desc'; | |
} | |
if($field == $parameters['field']){ | |
$icon = sprintf('fa %s-%s', $icon, $parameters['sort']); | |
} else{ | |
$icon = sprintf('fa %s', $icon); | |
} | |
return sprintf('<a href="%s">%s <i class="%s"></i></a>', route($name, $parameters), $title, $icon);} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you explain more ? how to implement in > L5 .. thanks..