DataTablesController.php 3.5 KB

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