/** * @return mixed */ public function build() { /** * @var BaseField $field * @var BaseField $clone * @var \Illuminate\Database\Eloquent\Model $items */ $indexPlanner = $this->getPlanner(); $model = $this->getModel(); $items = $model::with([]); $primaryKey = (new $model())->getKeyName(); // Field ordering if (Input::has('_order_by')) { $items->orderBy(Input::get('_order_by'), Input::get('_order')); } // Field filters foreach (Input::all() as $key => $value) { if (empty($value) || substr($key, 0, 1) === '_' || $key === 'page') { continue; } $items->where($key, 'LIKE', '%' . $value . '%'); } // Result scopes if (Input::has('_scope')) { $items->{Input::get('_scope')}(); } $items = $items->paginate($indexPlanner->getCRUDMasterInstance()->getPerPage()); $this->setPaginator($items); $result = []; foreach ($items as $item) { $row = new IndexResult(); $row->setIdentifier($item->{$primaryKey}); foreach ($indexPlanner->getFields() as $field) { $clone = clone $field; $name = $clone->getName(); $value = $item->{$name}; if ($clone->hasBefore()) { $before = $clone->getBefore(); $value = $before($value); } if ($clone->checkIfMultiple()) { $value = Value::decode(Config::get('crud.multiple-serializer'), $value); $value = implode(', ', $value); } $clone->setContext(BaseField::CONTEXT_INDEX)->setRowId($item->{$primaryKey})->setValue($value); $row->addField($name, $clone); } $result[] = $row; } $this->setResult($result); }
/** * Update a model from input. * * @param Request $request * * @return FormBuilder */ public function update($request) { $mapper = $this->getPlanner(); $admin = $mapper->getCRUDMasterInstance(); /** @var Model $model */ $model = $this->getModel(); $this->setRequest($request); // Field pre update /** @var BaseField $field */ foreach ($this->getPlanner()->getFields() as $field) { $field->preSubmitHook(); /* Is this a multiple field? */ if ($field->checkIfMultiple()) { $value = Value::encode(Config::get('crud::multiple-serializer'), $request->input($field->getName())); $this->getRequest()->offsetSet($field->getName(), $value); } if ($field->hasSaving()) { $saving = $field->getSaving(); $this->getRequest()->offsetSet($field->getName(), $saving($request->input($field->getName()))); } } /* Model before update hook */ if (method_exists($admin, 'beforeUpdate')) { $admin->beforeUpdate(); } /* Validate */ if (property_exists($model, 'rulesOnUpdate')) { $validator = Validator::make($this->getRequest()->all(), $model::$rulesOnUpdate); if ($validator->fails()) { return $validator; } } /* Model update hook */ if (method_exists($this->getPlanner()->getCRUDMasterInstance(), 'update')) { $this->getPlanner()->getCRUDMasterInstance()->update($this->getRequest(), $model::find($this->getIdentifier())); } else { $model::find($this->getIdentifier())->update($this->getRequest()->input()); } /* Field post update */ foreach ($this->getPlanner()->getFields() as $field) { $field->postSubmitHook($model::find($this->getIdentifier())); } /* Model after update hook */ if (method_exists($admin, 'afterUpdate')) { $result = $admin->afterUpdate($this->getRequest(), $model::find($this->getIdentifier())); if ($result instanceof RedirectResponse) { $result->send(); } } return $this; }