/** * Update an existing model. * * @param array Data to update a model * * @return bool */ public function store(array $data) { if ($data['image'] == 'delete') { $data['image'] = null; } if (Request::hasFile('image')) { $file = FileUpload::handle(Request::file('image'), 'uploads/settings'); $data['image'] = $file['filename']; } foreach ($data as $group_name => $array) { if (!is_array($array)) { $array = [$group_name => $array]; $group_name = 'config'; } foreach ($array as $key_name => $value) { $model = $this->model->where('key_name', $key_name)->where('group_name', $group_name)->first(); $model = $model ? $model : new $this->model(); $model->group_name = $group_name; $model->key_name = $key_name; $model->value = $value; $model->save(); } } return true; }
/** * On save, upload files. * * @param Model $model eloquent * * @return mixed false or void */ public function saving(Model $model) { if (!($attachments = $model->attachments)) { return; } foreach ($attachments as $fieldname) { if (Request::hasFile($fieldname)) { // delete prev image $file = FileUpload::handle(Request::file($fieldname), 'uploads/' . $model->getTable()); $model->{$fieldname} = $file['filename']; if ($model->getTable() == 'files') { $model->fill($file); } } else { if ($model->{$fieldname} == 'delete') { $model->{$fieldname} = null; } else { $model->{$fieldname} = $model->getOriginal($fieldname); } } } }