Skip to content

Commit

Permalink
Adding method to allow server side index & filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
nWidart committed Oct 12, 2017
1 parent e4df7bf commit 77ac9b3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Modules/User/Repositories/Sentinel/SentinelUserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Cartalyst\Sentinel\Laravel\Facades\Activation;
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Modules\User\Entities\Sentinel\User;
use Modules\User\Events\UserHasRegistered;
Expand Down Expand Up @@ -188,6 +191,39 @@ public function findByCredentials(array $credentials)
return Sentinel::findByCredentials($credentials);
}

/**
* Paginating, ordering and searching through pages for server side index table
* @param Request $request
* @return LengthAwarePaginator
*/
public function serverPaginationFilteringFor(Request $request): LengthAwarePaginator
{
$roles = $this->allWithBuilder();

if ($request->get('search') !== null) {
$term = $request->get('search');
$roles->where('first_name', 'LIKE', "%{$term}%")
->orWhere('last_name', 'LIKE', "%{$term}%")
->orWhere('email', 'LIKE', "%{$term}%")
->orWhere('id', $term);
}

if ($request->get('order_by') !== null && $request->get('order') !== 'null') {
$order = $request->get('order') === 'ascending' ? 'asc' : 'desc';

$roles->orderBy($request->get('order_by'), $order);
} else {
$roles->orderBy('created_at', 'desc');
}

return $roles->paginate($request->get('per_page', 10));
}

public function allWithBuilder() : Builder
{
return $this->user->newQuery();
}

/**
* Hash the password key
* @param array $data
Expand Down
9 changes: 9 additions & 0 deletions Modules/User/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Modules\User\Repositories;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\Request;
use Modules\User\Entities\UserInterface;

/**
Expand Down Expand Up @@ -78,4 +80,11 @@ public function delete($id);
* @return mixed
*/
public function findByCredentials(array $credentials);

/**
* Paginating, ordering and searching through pages for server side index table
* @param Request $request
* @return LengthAwarePaginator
*/
public function serverPaginationFilteringFor(Request $request) : LengthAwarePaginator;
}

0 comments on commit 77ac9b3

Please sign in to comment.