Adding method to allow server side index & filtering

parent e4df7bfe
......@@ -4,6 +4,9 @@ namespace Modules\User\Repositories\Sentinel;
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;
......@@ -188,6 +191,39 @@ class SentinelUserRepository implements UserRepository
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
......
......@@ -2,6 +2,8 @@
namespace Modules\User\Repositories;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\Request;
use Modules\User\Entities\UserInterface;
/**
......@@ -78,4 +80,11 @@ interface UserRepository
* @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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment