Skip to content

Commit

Permalink
add random sorting feature
Browse files Browse the repository at this point in the history
  • Loading branch information
selahattinunlu committed Oct 12, 2015
1 parent a4588d1 commit 5f8733d
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Unlu\Laravel\Api;

use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Unlu\Laravel\Api\Exceptions\UnknownColumnException;
use Unlu\Laravel\Api\UriParser;
use Exception;

class QueryBuilder
{
Expand Down Expand Up @@ -197,6 +198,11 @@ private function setOrderBy($order)

private function appendOrderBy($order)
{
if ($order == 'random') {
$this->orderBy[] = 'random';
return;
}

list($column, $direction) = explode(',', $order);

$this->orderBy[] = [
Expand Down Expand Up @@ -237,8 +243,12 @@ private function addWhereToQuery($where)
$this->query->where($key, $operator, $value);
}

private function addOrderByToQuery(array $order)
private function addOrderByToQuery($order)
{
if ($order == 'random') {
return $this->query->orderBy(DB::raw('RAND()'));
}

extract($order);

$this->query->orderBy($column, $direction);
Expand Down

0 comments on commit 5f8733d

Please sign in to comment.