| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Http\Request;
- use Yajra\DataTables\Facades\DataTables;
- class DataTablesController extends Controller
- {
- public function dynamicDataTable($datatableName, Request $request)
- {
- // Fetch the DataTable configuration
- $datatableConfig = DB::table('datatables')->where('name', $datatableName)->first();
- if (!$datatableConfig) {
- return response()->json(['error' => 'DataTable configuration not found'], 404);
- }
- // Determine the type of response (default to 'data')
- $type = $request->get('type', 'data');
- // Start building the query
- $query = DB::table($datatableConfig->base_table);
- // Apply columns
- $select_columns = json_decode($datatableConfig->select_columns, true);
- foreach ($select_columns as $column) {
- $query->selectRaw($column['select_exp']);
- }
- // Apply joins
- $joins = json_decode($datatableConfig->joins, true);
- if (!empty($joins)) {
- foreach ($joins as $join) {
- if (str_contains($join['table'], '(')) { // Detect subquery joins
- $query->join(DB::raw($join['table']), $join['on']['first'], $join['on']['operator'], $join['on']['second'], $join['type']);
- } else {
- $query->join($join['table'], $join['on']['first'], $join['on']['operator'], $join['on']['second'], $join['type']);
- }
- }
- }
- // Apply conditions (filters)
- $conditions = json_decode($datatableConfig->conditions, true);
- if (!empty($conditions)) {
- foreach ($conditions as $condition) {
- if (isset($condition['raw'])) {
- $query->whereRaw($condition['raw']);
- } else {
- $query->where($condition['column'], $condition['operator'], $condition['value']);
- }
- }
- }
- //Apply search filters
- $searchTerms = $request->input('searchTerms', [] );
- $columnHeaders = json_decode($datatableConfig->column_headers, true);
- $columnConfig = json_decode($datatableConfig->columns_config, true);
- foreach ($searchTerms as $index => $value) {
- $columnHeader = $columnHeaders[$index];
- $config = $columnConfig[$index];
- $columnName = $config['name'];
- $searchType = $columnHeader['searchtype'];
- if ($searchType === 'date' && is_array($value)) {
- if (!empty($value['start'])) {
- $query->whereDate($columnName, '>=', $value[$index]['start']);
- }
- if (!empty($value['end'])) {
- $query->whereDate($columnName, '<=', $value[$index]['end']);
- }
- } elseif (!empty($value)) {
- switch ($searchType) {
- case 'text':
- $query->where($columnName, 'like', '%' . $value[$index] . '%');
- break;
- case 'number':
- $query->where($columnName, '=', (int) $value[$index]);
- break;
- }
- }
- }
- // Apply sorting
- $sorting = $request->order[0] ?? json_decode($datatableConfig->default_sorting, true)[0];
- $query->orderBy($sorting['name'], $sorting['dir']);
- // Pass query to DataTables
- return DataTables::of($query)->make(true);
- }
- }
|