/**
  * Returns a Page of entities meeting the paging restriction provided in the Pageable object.
  *
  * @param Pageable $pageable
  *
  * @return Page
  */
 public function findAll(Pageable $pageable = null)
 {
     $model = self::$instance;
     $query = $model->query();
     if ($pageable) {
         $fields = $pageable->fields();
         $columns = !$fields->isNull() ? $fields->get() : ['*'];
         if (count($distinctFields = $pageable->distinctFields()->get()) > 0) {
             $query->getQuery()->distinct();
             $columns = $distinctFields;
         }
         $filter = $pageable->filters();
         if (!$filter->isNull()) {
             EloquentFilter::filter($query, $filter);
         }
         $sort = $pageable->sortings();
         if (!$sort->isNull()) {
             EloquentSorter::sort($query, $sort);
         }
         $pageSize = $pageable->pageSize();
         $pageSize = $pageSize > 0 ? $pageSize : 1;
         return new ResultPage($query->paginate($pageable->pageSize(), $columns, 'page', $pageable->pageNumber())->items(), $query->paginate()->total(), $pageable->pageNumber(), ceil($query->paginate()->total() / $pageSize));
     }
     return new ResultPage($query->paginate($query->paginate()->total(), ['*'], 'page', 1)->items(), $query->paginate()->total(), 1, 1);
 }
 /**
  * Returns all instances of the type meeting $distinctFields values.
  *
  * @param Fields      $distinctFields
  * @param Filter|null $filter
  * @param Sort|null   $sort
  *
  * @return array
  */
 public function findByDistinct(Fields $distinctFields, Filter $filter = null, Sort $sort = null)
 {
     $model = self::$instance;
     $query = $model->query();
     $columns = count($fields = $distinctFields->get()) > 0 ? $fields : ['*'];
     if ($filter) {
         EloquentFilter::filter($query, $filter);
     }
     if ($sort) {
         EloquentSorter::sort($query, $sort);
     }
     return $query->getQuery()->distinct()->get($columns);
 }