Adding new method to fetch roles for server side table

parent 9623e665
......@@ -2,6 +2,9 @@
namespace Modules\User\Repositories;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\Request;
/**
* Interface RoleRepository
* @package Modules\User\Repositories
......@@ -48,4 +51,11 @@ interface RoleRepository
* @return mixed
*/
public function findByName($name);
/**
* Paginating, ordering and searching through pages for server side index table
* @param Request $request
* @return LengthAwarePaginator
*/
public function serverPaginationFilteringFor(Request $request) : LengthAwarePaginator;
}
......@@ -3,6 +3,9 @@
namespace Modules\User\Repositories\Sentinel;
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Modules\User\Events\RoleIsCreating;
use Modules\User\Events\RoleIsUpdating;
use Modules\User\Events\RoleWasCreated;
......@@ -30,6 +33,33 @@ class SentinelRoleRepository implements RoleRepository
return $this->role->all();
}
/**
* 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('name', 'LIKE', "%{$term}%")
->orWhere('slug', '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));
}
/**
* Create a role resource
* @return mixed
......@@ -95,4 +125,12 @@ class SentinelRoleRepository implements RoleRepository
{
return Sentinel::findRoleByName($name);
}
/**
* @inheritdoc
*/
public function allWithBuilder() : Builder
{
return $this->role->newQuery();
}
}
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