templates=new Collection(); } public function setTemplatesId($id) { $this->nowId=$id; if (!$this->templates->has($id)) { $this->templates->put($id, new Collection()); } return $this; } protected function pushField(Field $field) { $this->templates->get($this->nowId)->push($field); } protected function getHtmlAndScript() { $htmls = []; $scripts = []; /* @var Field $field */ foreach ($this->templates as $key => $template) { $html=''; $script=[]; foreach ($template as $field) { //when field render, will push $script to Admin $html.= $field->render(); /* * Get and remove the last script of Admin::$script stack. */ if ($field->getScript()) { $script[] = array_pop(Admin::$script); } } $htmls[$key]=$html; $scripts[$key]=implode("\r\n", $script); } return [$htmls, $scripts]; } /** * Add nested-form fields dynamically. * * @param string $method * @param array $arguments * * @return mixed */ public function __call($method, $arguments) { if ($className = Form::findFieldClass($method)) { $column = array_get($arguments, 0, ''); /* @var Field $field */ $field = new $className($column, array_slice($arguments, 1)); $this->pushField($field); return $field; } return $this; } public function render() { list($template, $script) = $this->getHtmlAndScript(); return view('admin.field.template')->with('templates', $template)->with('scripts', $script)->render(); } }