DataTablesController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Database\Eloquent\Collection;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Http\Request;
  6. use Yajra\DataTables\Facades\DataTables;
  7. class DataTablesController extends Controller
  8. {
  9. public function dynamicDataTable($datatableName, Request $request)
  10. {
  11. // Fetch the DataTable configuration
  12. $datatableConfig = DB::table('datatables')->where('name', $datatableName)->first();
  13. if (!$datatableConfig) {
  14. return response()->json(['error' => 'DataTable configuration not found'], 404);
  15. }
  16. // Determine the type of response (default to 'data')
  17. $type = $request->get('type', 'data');
  18. // Start building the query
  19. $query = DB::table($datatableConfig->base_table);
  20. // Apply columns
  21. $select_columns = json_decode($datatableConfig->select_columns, true);
  22. foreach ($select_columns as $column) {
  23. $query->selectRaw($column['select_exp']);
  24. }
  25. // Apply joins
  26. $joins = json_decode($datatableConfig->joins, true);
  27. if (!empty($joins)) {
  28. foreach ($joins as $join) {
  29. if (str_contains($join['table'], '(')) { // Detect subquery joins
  30. $query->join(DB::raw($join['table']), $join['on']['first'], $join['on']['operator'], $join['on']['second'], $join['type']);
  31. } else {
  32. $query->join($join['table'], $join['on']['first'], $join['on']['operator'], $join['on']['second'], $join['type']);
  33. }
  34. }
  35. }
  36. // Apply conditions (filters)
  37. $conditions = json_decode($datatableConfig->conditions, true);
  38. if (!empty($conditions)) {
  39. foreach ($conditions as $condition) {
  40. if (isset($condition['raw'])) {
  41. $query->whereRaw($condition['raw']);
  42. } else {
  43. $query->where($condition['column'], $condition['operator'], $condition['value']);
  44. }
  45. }
  46. }
  47. $columnHeaders = json_decode($datatableConfig->column_headers, true);
  48. $columnConfig = json_decode($datatableConfig->columns_config, true);
  49. //Get Data for Dropdown header columns
  50. $dropdownData = new Collection();
  51. foreach ($columnHeaders as $index => $columnHeader) {
  52. if($columnHeader['searchtype'] === 'dropdown')
  53. {
  54. $ddColumnConfig = $columnConfig[$index];
  55. $columnSnapshot = (clone $query)
  56. ->select($ddColumnConfig['name'])
  57. ->distinct()->orderBy($ddColumnConfig['name'])
  58. ->pluck($ddColumnConfig['name']);
  59. $dropdownData[$index] = $columnSnapshot;
  60. }
  61. }
  62. //Apply search filters
  63. $searchTerms = $request->input('searchTerms', []);
  64. foreach ($searchTerms as $index => $value) {
  65. $columnHeader = $columnHeaders[$index];
  66. $config = $columnConfig[$index];
  67. $columnName = $config['name'];
  68. $searchType = $columnHeader['searchtype'];
  69. if ($searchType == 'date' && is_array($value)) {
  70. if (!empty($value[$index]['start'])) {
  71. $query->whereDate($columnName, '>=', $value[$index]['start']);
  72. }
  73. if (!empty($value[$index]['end'])) {
  74. $query->whereDate($columnName, '<=', $value[$index]['end']);
  75. }
  76. } elseif (!empty($value)) {
  77. switch ($searchType) {
  78. case 'text':
  79. $query->where($columnName, 'like', '%' . $value[$index] . '%');
  80. break;
  81. case 'number':
  82. $query->where($columnName, '=', (int) $value[$index]);
  83. break;
  84. case 'dropdown':
  85. if($value[$index] != '') {
  86. $query->where($columnName, '=', $value[$index]);
  87. }
  88. break;
  89. }
  90. }
  91. }
  92. // Apply sorting
  93. $sorting = $request->order[0] ?? json_decode($datatableConfig->default_sorting, true)[0];
  94. $query->orderBy($sorting['name'], $sorting['dir']);
  95. // Pass query to DataTables
  96. return DataTables::of($query)->with(['dropdowndata' => $dropdownData, 'searchterms' => $searchTerms])->make(true);
  97. }
  98. }