public function postStore($id = null) { // ---------------------------------------- HANDLE REQUEST ---------------------------------------- // handle id if (!is_null($id)) { $data = $this->model->findorfail($id); } else { $data = $this->model->newInstance(); } // ---------------------------------------- CHECK TAG ---------------------------------------- $tags_in_db = \App\Tag::whereIn('tag', Input::get('tags'))->get(); if (!$tags_in_db) { $tags_in_db = new Collection(); } foreach (Input::get('tags') as $x) { if (!$tags_in_db->where('tag', $x)->first()->id) { $new_tag = new \App\Tag(['tag' => $x]); if (!$new_tag->save()) { dd($new_tag->getErrors()); } $tags_in_db->push($new_tag); } } // ---------------------------------------- HANDLE SAVE ---------------------------------------- $input = Input::all(); if (!empty($input['published_at'])) { $input['published_at'] = \Carbon\Carbon::createFromFormat('d/m/Y H:i', $input['published_at'])->format('Y-m-d H:i:s'); } else { $input['published_at'] = null; } unset($input['longlat']); $input['tag_ids'] = $tags_in_db->pluck('id')->toArray(); $data->fill($input); if ($data->save()) { if (!$this->save_required_images($data, $input)) { return redirect()->back()->withInput()->withErrors($data->getErrors()); } return redirect()->route('admin.' . $this->view_name . '.show', ['id' => $data->id])->with('alert_success', '"' . $data->{$data->getNameField()} . '" has been saved successfully'); } else { return redirect()->back()->withInput()->withErrors($data->getErrors()); } }