public function autoUpdate($save = false) { $this->getValue(); if ($this->action == "update" || $this->action == "insert") { if (Input::hasFile($this->name)) { $this->file = Input::file($this->name); $filename = $this->filename != '' ? $this->filename : $this->file->getClientOriginalName(); $uploaded = $this->file->move($this->path, $filename); if ($uploaded && is_object($this->model) && isset($this->db_name)) { if (!Schema::hasColumn($this->model->getTable(), $this->db_name)) { return true; } $this->new_value = $filename; if (isset($this->new_value)) { $this->model->setAttribute($this->db_name, $this->new_value); } else { $this->model->setAttribute($this->db_name, $this->value); } if ($save) { return $this->model->save(); } } } } return true; }
/** * @param $column * @return bool */ private function columnExists($column) { if (!isset($this->sortable)) { return Schema::hasColumn($this->getTable(), $column); } else { return in_array($column, $this->sortable); } }
public function all() { $query = $this->model; if (Schema::hasColumn(str_plural($this->modelName), 'published_at')) { $query = $query->where('is_published', 1)->where('published_at', '<=', Carbon::now()->format('Y-m-d h:i:s')); } return $query->orderBy('created_at', 'desc')->paginate(Config::get('quarx.pagination', 25)); }
/** * Reverse the migrations. * * @return void */ public function down() { if (!Schema::hasColumn('short_term_trainings', 'competency_id')) { Schema::table('short_term_trainings', function (Blueprint $table) { $table->dropColumn('competency_id'); }); } }
/** * Run the migrations. * * @return void */ public function up() { Schema::table('videos', function (Blueprint $table) { if (Schema::hasColumn('videos', 'config')) { $table->dropColumn('config'); } }); }
public static function renameColumn($tableName, $oldColumnName, $newColumnName, $type, $length, $unsigned, $nullable, $default = null) { if (Schema::hasColumn($tableName, $oldColumnName)) { Schema::table($tableName, function (Blueprint $table) use($tableName, $oldColumnName, $newColumnName, $type, $length, $unsigned, $nullable, $default) { $sql = DBLibrary::getAlterTableSql($tableName, $oldColumnName, $newColumnName, $type, $length, $unsigned, $nullable, $default); DB::select(DB::raw($sql)); }); } }
protected function entryRelationDisplayColumn() { $relation_display_column = $this->commandObj->ask('Please input column name shown in index page.(If you want to cancel, enter "cancel")'); if ($relation_display_column == 'cancel') { $this->commandObj->info('make:relation canceled!'); exit; } else { if (!Schema::hasColumn($this->model_A_tablename, $relation_display_column)) { $this->commandObj->error('Column "' . $relation_display_column . '" is not exist in ' . $this->model_A_tablename . ' tables'); $this->entryRelationDisplayColumn(); } else { return $relation_display_column; } } }
private function _exec_table_builder($table_name, $fields_to_add) { $table_name = $this->assoc_table_name($table_name); if (!Schema::hasTable($table_name)) { Schema::create($table_name, function ($table) { $table->increments('id'); }); } if (is_array($fields_to_add)) { Schema::table($table_name, function ($schema) use($fields_to_add, $table_name) { foreach ($fields_to_add as $name => $meta) { $is_index = substr($name, 0, 1) === '$'; $is_default = null; $is_nullable = true; if (!$is_index) { if (is_array($meta)) { if (!isset($meta['type'])) { $name = array_shift($meta); $type = array_shift($meta); } else { $type = $meta['type']; } if (isset($meta['default'])) { $is_default = $meta['default']; if ($is_default == 'not_null') { $is_nullable = false; } } } else { $type = $meta; } if (!Schema::hasColumn($table_name, $name)) { $fluent = $schema->{$type}($name); if ($is_default !== null) { $fluent->default($is_default)->nullable(); } if ($is_nullable) { $fluent->nullable(); } } } } }); } }
/** * Match columns in the database */ public function matchColumns() { Schema::table($this->name, function ($table) { $table->engine = 'InnoDB'; // Loop through all table columns and check if they exists in the database already. // If one of them doesn't exists then create it foreach ($this->columns as $column => $generator) { // If column doesn't exist then create it if (!Schema::hasColumn($this->getName(), $column)) { call_user_func($generator, $table); } } foreach ($this->getTableColumns() as $dbColumn) { if (!isset($this->columns[$dbColumn->Field])) { $table->dropColumn($dbColumn->Field); } } }); }
public function compose($view) { $defaultLanguage = Cache::get('default_language'); $viewName = $view->getName(); $pos = strrpos($viewName, '.'); $name = substr($viewName, $pos + 1); if (preg_match('/^submenu_/i', $name)) { $target = preg_split('/^submenu_/i', $name); $table = 'layout_' . $target[1]; if (Schema::hasTable($table)) { $db = new Content($table); if (Schema::hasColumn($table, 'order')) { $items = $db->language($defaultLanguage->id)->active()->orderBy('order')->get(); } elseif (Schema::hasColumn($table, 'published_date')) { $items = $db->language($defaultLanguage->id)->active()->orderBy('published_date', 'desc')->get(); } $view->with(compact('items')); } } if ($name == 'menu') { $table = 'menus'; if (Schema::hasTable($table)) { $db = new Content($table); $result = $db->language($defaultLanguage->id)->active()->orderBy('order')->get(); $groupId = $db->whereType('group')->whereDisplay('Subjects')->first(); $menuItems = $result->filter(function ($item) { return $item->group_id == 1; }); $view->with(compact('menuItems')); } } else { $table = 'partial_' . $name; if (Schema::hasTable($table)) { $db = new Content($table); $content = $db->language($defaultLanguage->id)->active()->first(); $view->with($table, $content); } } $view; }
public function createTree($model = false, $act = false) { $this->tree = array(); $query = $act ? self::where('act', 1) : self::newQuery(); if (Schema::hasColumn($this->table, 'path')) { $list = $query->orderBy('parent', 'asc')->orderBy('sort', 'asc')->get(); } else { $siteClass = config('model.site') ?: 'Model\\Site'; $siteTable = (new $siteClass())->getTable(); $query->select($this->table . '.*', "{$siteTable}.path"); $list = $query->leftJoin($siteTable, "{$this->table}.site_id", '=', "{$siteTable}.id")->orderBy("{$this->table}.parent", 'asc')->orderBy("{$this->table}.sort", 'asc')->get(); } foreach ($list as $item) { $this->listTree[$item->id] = $item; $this->parentTree[$item->parent][] = $item->id; } if (!empty($this->parentTree[0]) && is_array($this->parentTree[0])) { foreach ($this->parentTree[0] as $k => $item) { $this->_branch($item, $this->tree[$k], $model); } } return $this->tree; }
protected function prepareSelectValues() { $this->db->select($this->getOptionDB('table') . '.id'); $def = $this->controller->getDefinition(); if (isset($def['options']['is_sortable']) && $def['options']['is_sortable']) { if (!Schema::hasColumn($this->getOptionDB('table'), "priority")) { Schema::table($this->getOptionDB('table'), function ($table) { $table->integer("priority"); }); } $this->db->addSelect($this->getOptionDB('table') . '.priority'); } $fields = $this->controller->getFields(); foreach ($fields as $name => $field) { $field->onSelectValue($this->db); } }
protected function doCreateField($table_name, $field_name) { $field_bd = $this->getAttribute('field'); $data = Session::all(); if (!Session::has($table_name . '.' . $field_name)) { if ($field_bd && !Schema::hasColumn($table_name, $field_name)) { Session::push($table_name . '.' . $field_name, 'created'); @(list($field, $param) = explode("|", $field_bd)); Schema::table($table_name, function ($table) use($field_name, $field, $param) { $field_add = $table->{$field}($field_name); if ($param) { $field_add->length($param); } }); } else { Session::push($table_name . '.' . $field_name, 'created'); } } }
/** * @param $model * @param $column * * @return bool */ private function columnExists($model, $column) { return isset($model->sortable) ? in_array($column, $model->sortable) : Schema::hasColumn($model->getTable(), $column); }
/** * Drop a column from a table. * * @param $tableName * @param $column */ public function dropColumn($tableName, $column) { // Check for its existence before dropping if (Schema::hasColumn($tableName, $column)) { Schema::table($tableName, function (Blueprint $table) use($column) { $table->dropColumn($column); }); } }
private function hasTableColumn($column, $model = null) { $model = !is_null($model) ? $model : $this->model; return Schema::hasColumn($model->getTable(), $column); }
public function dropPiggyBankIdFromTransactions() { Schema::table('transactions', function (Blueprint $table) { if (Schema::hasColumn('transactions', 'piggybank_id')) { $table->dropForeign('transactions_piggybank_id_foreign'); $table->dropColumn('piggybank_id'); } }); }
private function hasTableColumn($column) { return Schema::hasColumn($this->model->getTable(), $column); }
public function autoUpdate($save = false) { $this->getValue(); $this->getNewValue(); if (is_object($this->model) && isset($this->db_name)) { if (!Schema::hasColumn($this->model->getTable(), $this->db_name)) { $this->model->saved(function () { $this->updateRelations(); }); //check for relation then exit return true; } if (isset($this->new_value)) { $this->model->setAttribute($this->db_name, $this->new_value); } else { $this->model->setAttribute($this->db_name, $this->value); } if ($save) { return $this->model->save(); } } return true; }
/** * Validates field names */ protected function validateFields() { foreach (array_keys($this->fields) as $field) { if ($this->isTable()) { if (!Schema::hasColumn($this->table, $field)) { $this->abort("Column '{$field}' doesn't exist."); } } else { // isModel if (!in_array($field, $this->model->getFillable()) || in_array($field, $this->model->getGuarded())) { $this->abort("Attribute '{$field}' doesn't exist or is not fillable."); } } } }
public function dropColums($tableName, $columns) { $tempColumns = []; if ($columns instanceof Collection) { foreach ($columns as $column) { $tempColumns[] = $column->code; } } if (is_array($columns)) { foreach ($columns as $column) { $tempColumns[] = $column["code"]; } } foreach ($tempColumns as $targetColumn) { if (Schema::hasColumn($tableName, $targetColumn)) { Schema::table($tableName, function ($table) use($targetColumn) { $table->dropColumn($targetColumn); }); } } }
/** * @param string $recordId * @param array $postData * @return array The updated resource */ public function updateResource($recordId, $postData) { $collection = $this->getThisCollection(); $entityName = $collection->getEntityClassName(); try { $model = App::make($entityName)->findOrFail($recordId); } catch (ModelNotFoundException $exc) { throw new Exception('Object not found for this recordId'); } $attributes = $this->getAttributesAndRelations($postData); foreach ($attributes as $property => $value) { if (Schema::hasColumn($model->getTable(), $property)) { if ($collection->hasField($property)) { $fieldType = $collection->getField($property)->getType(); if ($fieldType == 'Date') { $value = Carbon::instance(new DateTime($value)); } } $model->{$property} = $value; } } $model->save(); return $recordId; }
protected function _record($model, $action) { foreach (Input::all() as $key => $value) { if ((empty($this->writableFields) || in_array($key, $this->writableFields)) && (empty($this->forbiddenFields) || !in_array($key, $this->forbiddenFields)) && Schema::hasColumn($model->getTable(), $key)) { $model->{$key} = $value; } } if ($model->save()) { $this->result['action'] = $action; if (Input::get('_target')) { $this->result['content'] = (string) view($this->templatePath() . '.item', [$this->modelName => $model]); } } else { $this->result['errors'] = $model->getError(); $this->result['messages'] = $model->getValidationMessages(); } return $this->result(); }
public function scopeFiltroComunidades($query, Request $request) { foreach ($request->all() as $idx => $val) { if (Schema::hasColumn('comunidades', $idx) && $val != null && trim($val) != '') { if (is_numeric($val)) { $query->where($idx, $val); } else { $query->where($idx, 'LIKE', "{$val}" . '%'); } } } return $query; }
/** * Show trashedElements (only 'mysql') * @param type $action * @return type */ protected function trashedElements() { $tables = \Illuminate\Support\Facades\DB::select('SHOW TABLES'); $items = array(); foreach ($tables as $table) { if (\Illuminate\Support\Facades\Schema::hasColumn(reset($table), 'deleted_at')) { $check = \Illuminate\Support\Facades\DB::table(reset($table))->whereNotNull('deleted_at')->count(); if ($check > 0) { $items[reset($table)] = $check; } } } return $items; }