$success, 'data' => $data, 'message' => $message, 'code' => $success ? 200 : 500, 'recordsTotal' => $items['total'], 'recordsFiltered' => $items['totalFiltered'], ]; \Debugbar::disable(); echo json_encode($json); return $json; exit; return response()->json($json); } public static function get($data, $options = array()) { // Debug::fdump($options); $model = self::getModel($options); $select = isset($options['select']) ? $options['select'] : ''; $elements = new $model(); $table = $elements->getTable(); $real_id = $table . '.id'; $data2 = static::getElements($data, $options); // Debug::fdump($data2); $elements = $data2['elements']; $length = $data2['length']; $skip = $data2['skip']; $order = $data2['order']; $sort = $data2['sort']; // Debug::fdump($elements->get()->toArray()); // // Debug::fdump($order); // exit; if (strpos($order, '.')) { $tab = explode('.', $order); $nb_model = count($tab) - 1; for ($i = 0; $i < $nb_model; $i++) { $controller = $tab[$i]; if (isset($options['namespace'])) { $namespace = $options['namespace']; } else { $namespace = 'App\Models\\'; } $jointModelObj = $namespace.ucfirst(Str::camel($controller)); // Debug::fdump($controller); $jointModel = new $jointModelObj(); $jointTable = $jointModel->getTable(); // Debug::fdump($controller); // Debug::fdump($jointTable); if ($table !== $jointTable) { $elements = $elements->leftJoin($jointTable, $jointTable.'.id', '=', $controller.'_id'); } $table = $controller; } $order = $jointTable . '.' . $tab[$nb_model]; } /* if (!empty($select)) { $elements = $elements->select('*',"$real_id as id", $select); } else { $elements = $elements->select('*',"$real_id as id"); } */ // Debug::fdump($order); $elements = $elements->orderBy($order, $sort); if (!empty($options['order']) && ($options['order'] !== $order)) { $elements = $elements->orderBy($options['order'], $options['sort']); } // Debug::dump($elements); if (isset($options['trash']) && $options['trash']) { $elements = $elements->withTrashed()->take($length)->skip($skip)->get(); } else { $elements = $elements->take($length)->skip($skip)->get(); } // Debug::dump($elements); $tab = [ 'elements' => $elements->toArray(), 'total' => $data2['total'], 'totalFiltered' => $data2['totalFiltered'] ]; // dump($elements->toArray()); return $tab; } public static function getModel($options) { // return '\App\Models\\'.$options['app'].$options['model']; return $options['model']; } public static function getElements($data, $options) { $vars = static::QueryBuilder($data, $options); // Debug::fdump($vars); $model = self::getModel($options); // Debug::dump($model); $elements = new $model(); $total = $elements::count(); // Debug::dump($vars); // exit; if (is_array($vars)) { extract($vars); } // dump($order); if (empty($order)) { $order = $options['order']; $sort = $options['sort']; } $with = (isset($options['with'])) ? $options['with'] : null; $withCount = (isset($options['withCount'])) ? $options['withCount'] : null; $where = (isset($vars['where'])) ? $vars['where'] : null; $searchcol = (isset($vars['searchcol'])) ? $vars['searchcol'] : null; $filter = (isset($vars['filter'])) ? $vars['filter'] : null; Debug::dump($with); Debug::dump($withCount); Debug::dump($where); Debug::dump($searchcol); Debug::dump($filter); $elements = ($with) ? $elements->with($with) : $elements; $elements = ($withCount) ? $elements->withCount($withCount) : $elements; $elements = ($where) ? $elements->whereRaw($where) : $elements; $elements = ($filter) ? $elements->whereRaw($filter) : $elements; // Debug::fdump($elements->get()->toArray()); // Debug::message($where); // exit; $elements = static::addSearchFilter($elements, $hasfilters); $elements = static::addSearch($elements, $searchcol, $search); Debug::dump($hasfilters); // dump($search); // $totalFiltered = $elements->count(); // Debug::breakpoint($elements); // exit; $data2 = [ 'elements' => $elements, 'total' => $total, 'totalFiltered' => $totalFiltered, 'length' => $length, 'skip' => $skip, 'order' => $order, 'sort' => $sort, ]; // var_dump($data2['elements']->get()->toArray()); return $data2; } public static function addSearchFilter($elements, $hasfilters) { if (is_array($hasfilters)) { foreach ($hasfilters as $hasfilter) { if (!empty($hasfilter['search'])) { $elements = $elements->whereHas($hasfilter['controller'], function ($query) use ($hasfilter) { if ($hasfilter['like']) { $query->where($hasfilter['field'], 'like', '%' . $hasfilter['search'] . '%'); } else { $query->where($hasfilter['field'], '=', $hasfilter['search']); } }); } } } return $elements; } public static function addSearch($elements, $searchcol, $search) { if (!empty($search)) { if (strpos($searchcol, '.')) { $tab = explode('.', $searchcol); $searchField = [ 'controller' => $tab[0], 'field' => $tab[1], 'search' => $search, ]; $elements = $elements->whereHas($searchField['controller'], function ($query) use ($searchField) { $query->where($searchField['field'], 'like', '%' . $searchField['search'] . '%'); }); } else { $elements = $elements->where($searchcol, 'like', "%$search%"); } } return $elements; } public static function QueryBuilder($data, $options = array()) { $model = self::getModel($options); $elements = new $model(); $table = $elements->getTable(); $filter = ''; $hasfilters = array(); // Debug::fdump($data); // Debug::message($data); // Debug::dump($options); // Debug::dump($data); if (is_array($options) && is_array($options['likefields'])) { $likefields = $options['likefields']; } $length = isset($data['length']) ? (int) $data['length'] : 10; $skip = isset($data['start']) ? (int) $data['start'] : 0; if (isset($data['search'])) { $search = ($data['search']['value'] !== '') ? $data['search']['value'] : ''; } else { $search = null; } // Debug::fdump($data); if (isset($data['order'])) { if ($data['order'][0]['dir']) { //on est sur qu'un tri est en cours, pb de la colonne 0 $sort = ($data['order'][0]['dir']); $order = self::getSortcol($data); } } else { $order = null; $sort = null; } // Debug::dump($order); if (isset($data['columns']) && is_array($data['columns'])) { foreach ($data['columns'] as $item) { $filter_search = $item['search']['value']; $filter_col = ($item['name']) ? $item['name'] : $item['data']; // Debug::dump($filter_col); // Debug::dump($filter_search); // Debug::dump(is_null($filter_search)); if (!is_null($filter_search)) { // Debug::dump($item); // Debug::dump($filter_search); // Debug::dump($filter_col); if (strpos($filter_col, '.')) { $tab = explode('.', $filter_col); if (is_array($likefields) && in_array($filter_col, $likefields)) { $like = true; } else { $like = false; } $hasfilters[] = [ 'controller' => $tab[0], 'field' => $tab[1], 'search' => $filter_search, 'like' => $like, ]; } else { // $filter_col = $table . '.' .$item['data']; $filter .= (!empty($filter)) ? ' and ' : ''; if (is_array($likefields) && in_array($filter_col, $likefields)) { $filter .= "($table.$filter_col LIKE '%$filter_search%')"; } else { $filter .= "($table.$filter_col = '$filter_search')"; } } } } } if (isset($data['where'])) { $where = $data['where']; } else { $where = null; } $options = [ 'length' => $length, 'skip' => $skip, 'search' => $search, 'order' => $order, 'sort' => $sort, 'filter' => $filter, 'hasfilters' => $hasfilters, 'where' => $where, ]; Debug::dump($options); return $options; } public static function getSortcol($data) { $sortcol = $data['order'][0]['column']; if (!is_null($sortcol) && ($sortcol !== 0)) { if (!empty($data['columns'][$sortcol]['name'])) { $order = $data['columns'][$sortcol]['name']; } else { $order = $data['columns'][$sortcol]['data']; } } return $order; } }