Skip to content

Commit

Permalink
Update readme badge, update travis.yml, simplify queryJoinBuilder()
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyslik committed Feb 8, 2017
1 parent 45caad5 commit fa4de22
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ script: vendor/bin/phpunit

branches:
only:
- master
- L5.2
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
[![Latest Version](https://img.shields.io/github/release/Kyslik/column-sortable.svg?style=flat-square)](https://github.com/Kyslik/column-sortable/releases)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Total Downloads](https://img.shields.io/packagist/dt/Kyslik/column-sortable.svg?style=flat-square)](https://packagist.org/packages/Kyslik/column-sortable)
[![Build Status](https://travis-ci.org/Kyslik/column-sortable.svg?branch=master)](https://travis-ci.org/Kyslik/column-sortable)
[![Build Status](https://travis-ci.org/Kyslik/column-sortable.svg?branch=L5.2)](https://travis-ci.org/Kyslik/column-sortable)

Package for handling column sorting in Laravel 5.[1-4].

Expand Down
18 changes: 7 additions & 11 deletions src/ColumnSortable/Sortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function scopeSortable($query, $defaultSortParameters = null)
return $this->queryOrderBuilder($query, Request::only(['sort', 'order']));
} elseif (!is_null($defaultSortParameters)) {
$defaultSortArray = $this->formatToSortParameters($defaultSortParameters);

if (Config::get('columnsortable.allow_request_modification', true) && !empty($defaultSortArray)) {
Request::merge($defaultSortArray);
}
Expand Down Expand Up @@ -117,20 +117,16 @@ private function parseSortParameters(array $sortParameters)
*/
private function queryJoinBuilder($query, $relation)
{
$relatedModel = $relation->getRelated();
$relatedTable = $relatedModel->getTable();

$parentModel = $relation->getParent();
$parentTable = $parentModel->getTable();

$relatedTable = $relation->getRelated()->getTable();
$parentTable = $relation->getParent()->getTable();

if ($relation instanceof HasOne) {
$relatedPrimaryKey = $relation->getForeignKey();
$parentPrimaryKey = $parentTable . '.' . $parentModel->primaryKey;
$parentPrimaryKey = $relation->getQualifiedParentKeyName();
return $query->select($parentTable . '.*')->join($relatedTable, $parentPrimaryKey, '=', $relatedPrimaryKey);
} elseif ($relation instanceof BelongsTo) {
$relatedPrimaryKey = $relatedTable . '.' . $relatedModel->primaryKey;
$parentPrimaryKey = $parentTable . '.' . $relation->getForeignKey();
$relatedPrimaryKey = $relation->getQualifiedOtherKeyName();
$parentPrimaryKey = $relation->getQualifiedForeignKey();
return $query->select($parentTable . '.*')->join($relatedTable, $parentPrimaryKey, '=', $relatedPrimaryKey);
} else {
throw new \Exception();
Expand Down Expand Up @@ -168,7 +164,7 @@ private function formatToSortParameters($sort)

reset($sort);
$each = each($sort);

return ($each[0] === 0) ? ['sort' => $each[1], 'order' => $configDefaultOrder] :
['sort' => $each[0], 'order' => $each[1]];
}
Expand Down

0 comments on commit fa4de22

Please sign in to comment.