query()->select( 'id', 'name', 'created_at' )->where('name', '!=', config('access.users.super_admin_role')); $columns = ['','id', 'name', 'created_at']; if (isset($request['name']) && $request['name'] != '') { $searchQuery = $searchQuery->where('name', 'like', '%' . $request['name'] . '%'); } if (isset($request['order']) && $request['order'] != "") { $searchQuery = $searchQuery->orderBy( $columns[$request['order'][0]['column']], $request['order'][0]['dir'] ); } } catch (Exception $ex) { Log::error($ex->getMessage()); } return $searchQuery; } /** * This function is for the format the Datatable columns * * @param $request * @return object */ public function getForDataTable($request): object { $response = (object)[]; try { $dataTableQuery = $this->prepareSearchQuery($request); // ->orderBy('id', 'DESC'); $response = Datatables::of($dataTableQuery) // ->addColumn( // 'checkbox_action', // function ($role) { // return $role->role_checkbox_action; // } // ) ->addIndexColumn() ->addColumn( 'created_at', function ($user) { return (($user->created_at) ? defaultDateTimeFormat($user->created_at) : '-'); } ) ->addColumn( 'action', function ($role) { if ( !empty($role->roles[0]['name']) != config('access.users.admin_role') || !empty($role->roles[0]['name']) != config('access.users.super_admin_role') || loggedInUser()->isSuperAdmin() || loggedInUser()->isAdmin() ) { return $role->role_action_buttons; } return ""; } ) ->setRowId( function ($role) { return 'recordRow-' . $role->id; } ) ->rawColumns(['action', 'checkbox_action']) ->make(true); } catch (Exception $ex) { Log::error($ex->getMessage()); } return $response; } /** * This function is for the create role * * @param array $request * @return Role * @throws GeneralException */ public function create(array $request): Role { try { return DB::transaction( function () use ($request) { $role = $this->query()->create( [ 'name' => $request['name'], ] ); $role->syncPermissions($request['permissions']); return $role; } ); } catch (Exception $ex) { Log::error($ex->getMessage()); throw new GeneralException(__('message.create_role_error')); } } /** * This function is for the update role * * @param object $role * @param array $request * @return Role * @throws GeneralException */ public function update(object $role, array $request): Role { try { return DB::transaction( function () use ($role, $request) { $this->query()->where('id', $role->id)->update( [ 'name' => !empty($request['name']) ? $request['name'] : $role->name, ] ); $role->syncPermissions($request['permissions']); return $role; } ); } catch (Exception $ex) { Log::error($ex->getMessage()); throw new GeneralException(__('message.update_role_error')); } } /** * This function is for the multiple records action from the grid * * @param string $actionType * @param array $data * @return bool * @throws GeneralException */ public function gridActions(string $actionType, array $data): bool { try { switch ($actionType) { case Constant::STATUS_ACTIVE: $this->query()->whereIn('id', $data)->update(['status' => Constant::STATUS_ONE]); return Constant::STATUS_TRUE; case Constant::STATUS_INACTIVE: $this->query()->whereIn('id', $data)->update(['status' => Constant::STATUS_ZERO]); return Constant::STATUS_TRUE; case Constant::STATUS_DELETE: $this->query()->whereIn('id', $data)->delete(); return Constant::STATUS_TRUE; default: break; } } catch (Exception $ex) { Log::error($ex->getMessage()); throw new GeneralException(__('message.delete_role_error')); } } }