Skip to content

Commit

Permalink
refactor: make includes optional
Browse files Browse the repository at this point in the history
  • Loading branch information
esbenp committed Dec 24, 2015
1 parent ae15090 commit 7b1c3e6
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/EloquentBuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Optimus\Api\Controller;

use InvalidArgumentException;
use Illuminate\Database\Eloquent\Builder;

trait EloquentBuilderTrait
Expand All @@ -15,16 +16,26 @@ trait EloquentBuilderTrait
private function applyResourceOptions(Builder $query, array $options = [])
{
if (!empty($options)) {
$query->with($options['includes']);
extract($options);

if (isset($options['sort'])) {
$query->orderBy($options['sort']);
if (isset($includes)) {
if (!is_array($includes)) {
throw InvalidArgumentException('Includes should be an array.');
}

$query->with($includes);
}
if (isset($options['limit'])) {
$query->limit($options['limit']);

if (isset($sort)) {
$query->orderBy($sort);
}
if (isset($options['page'])) {
$query->offset($options['page']*$options['limit']);

if (isset($limit)) {
$query->limit($limit);
}

if (isset($page)) {
$query->offset($page*$limit);
}
}

Expand Down

0 comments on commit 7b1c3e6

Please sign in to comment.