/** * @return \Illuminate\Database\Eloquent\Builder */ protected function prepareModelQuery() { $tableColumns = $this->model->getAdminTableColumns(); $query = $this->model->newQuery(); $table = $this->model->getTable(); $keyName = $this->model->getKeyName(); // What if model key is composite if (is_array($keyName)) { $select = []; foreach ($keyName as $key) { $select[] = $table . '.' . $key; } $query->select($select); } else { $query->select([$table . '.' . $this->model->getKeyName()]); } $with = []; foreach ($tableColumns as $name => $column) { // We already included the primary key so check the column and do nothing if exists. if (is_array($keyName)) { if (in_array($column, $keyName)) { continue; } } else { if ($column == $this->model->getKeyName()) { continue; } } // If it's a relation we need to eager load it. if (strstr($column, '.') !== false) { $relation = explode('.', $column); $relationField = array_pop($relation); $with[] = implode('.', $relation); } else { $query->addSelect($table . '.' . $column); } } if (!empty($with)) { $query->with($with); // We should refactor this and find actual related field. $query->select($table . '.*'); } return $query; }