Skip to content

Commit

Permalink
Added the ability to set key-value pairs as $sortable
Browse files Browse the repository at this point in the history
It allows one to target, say, the “name” column in HTML, which actually
corresponds to the joined column “parties.official_name”: `’name’ =>
‘parties.official_name’`
  • Loading branch information
gbrock committed Aug 5, 2015
1 parent a0083dc commit 452034f
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Traits/Sortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function scopeSorted($query, $field = false, $direction = false)
}

// If the field requested isn't known to be sortable by our model, fail silently.
if(!in_array($field, (array) $this->sortable))
if(!in_array($field, (array) $this->sortable) && !isset($this->sortable[$field]))
{
return $query;
}
Expand All @@ -34,15 +34,20 @@ public function scopeSorted($query, $field = false, $direction = false)
$direction = config('gbrock-tables.default_direction');
}

if(in_array($field, $this->sortable))
if(isset($this->sortable[$field]))
{
// Set via key
$sortField = $this->sortable[$field];
}
elseif(in_array($field, $this->sortable))
{
// Does the passed field contain a period character?
$sortField = strpos($field, '.') === FALSE ? $this->getTable() . '.' . $field : $field;

// At this point, all should be well, continue.
return $query
->orderBy($sortField, $direction);
}

// At this point, all should be well, continue.
return $query
->orderBy($sortField, $direction);
}

public function getSortable()
Expand Down

0 comments on commit 452034f

Please sign in to comment.