Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. public function paginate(Request $request)
  2. {
  3. $params = $request->all();
  4. $department = Department::with('company', 'establishment', 'location');
  5. $path = 'department';
  6.  
  7. if (Auth()->user()->isCompanyAdmin() && !Auth()->user()->isNSUser())
  8. {
  9. $department = $department->where('departments.company_id', Auth()->user()->company_id);
  10. }
  11.  
  12. if ($request->input('columns') !== null) {
  13. foreach ($request->input('columns') as $index => $column) {
  14. if ($column['searchable'] === 'true') {
  15. $value = $column['search']['value'];
  16.  
  17. if($params['draw'] != 1 && $value == null)
  18. {
  19. $request->session()->forget($path . '.' . $index);
  20. }
  21.  
  22. if(strlen($value) > 0) {
  23. $request->session()->put($path . '.' . $index, $value);
  24. } else {
  25. if($params['draw'] == 1) {
  26. $value = $request->session()->get($path . '.' . $index);
  27. }
  28. }
  29.  
  30. if (strlen($value) > 0 && strcmp($value, 'null') != 0) {
  31. if (strpos($column['data'], '.') > -1) {
  32. $table = explode('.', $column['name'])[0];
  33. $field = explode('.', $column['data'])[1];
  34. if ($field == '[]') {
  35. $field = explode('.', $column['data'])[2];
  36. }
  37.  
  38. $name = explode('.', $params['columns'][$index]['data'])[0];
  39. $department = $department->whereHas($name, function ($q) use ($table, $field, $value) {
  40. $q->where($table . '.' . $field, 'LIKE', '%' . $value . '%');
  41. });
  42. } else {
  43. $department = $department->where('departments.' . $column['data'], 'LIKE', '%' . $value . '%');
  44. }
  45. }
  46. }
  47. }
  48. }
  49.  
  50. $params['order'] = $params['order'][0];
  51. if(isset($params['order']['column']) && isset($params['order']['dir'])) {
  52. if($params['draw'] == 1) {
  53. $column_name = $request->session()->get($path . '.order.column');
  54. $order = $request->session()->get($path . '.order.dir');
  55.  
  56. if(strlen($column_name) == 0) {
  57. $column_name = $params['columns'][$params['order']['column']]['name'];
  58. }
  59. if(strlen($order) == 0) {
  60. $order = $params['order']['dir'];
  61. }
  62. } else {
  63. $column_name = $params['columns'][$params['order']['column']]['name'];
  64. $order = $params['order']['dir'];
  65. $request->session()->put($path . '.order.column', $column_name);
  66. $request->session()->put($path . '.order.dir', $order);
  67. }
  68.  
  69. if (strpos($column_name, '.') === false) {
  70. $department = $department->orderBy($column_name, $order);
  71. } else {
  72. $table = explode('.', $column_name)[0];
  73. $name = "";
  74.  
  75. foreach($params['columns'] as $index => $column) {
  76. if($column_name == $column['name']) {
  77. $name = explode('.', $params['columns'][$index]['data'])[0];
  78. $request->session()->put($path . '.order.index', $index);
  79. }
  80. }
  81.  
  82. $department = $department->join($table, 'departments.' . $name . '_id', '=', $table . '.id')
  83. ->orderBy($table . '.name', $order)->select('departments.*');
  84. }
  85. }
  86.  
  87. $perPage = 20;
  88.  
  89. if(!empty($params['length'])) {
  90. $perPage = $params['length'];
  91. $request->session()->put($path.'.perPage', $perPage);
  92. }
  93.  
  94. $page = 1;
  95. if ($request->input('start') > 0)
  96. {
  97. $page = ($request->input('start') / $perPage) + 1;
  98. }
  99.  
  100. if($params['draw'] == 1) {
  101. $currentPage = session()->get($path . '.currentPage');
  102.  
  103. if(strlen($currentPage) > 0) {
  104. $page = $currentPage;
  105. }
  106. }
  107.  
  108. session()->put($path . '.currentPage', $page);
  109.  
  110. Paginator::currentPageResolver(function() use ($page) {
  111. return $page;
  112. });
  113.  
  114. $department = $department->paginate($perPage);
  115. return response($department)->header('X-Total-Count',$department->total());
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement