/** * Update the specified resource in storage. * * @param PostFormRequest $request * @param Post $post * @return Response */ public function update(PostFormRequest $request, $post) { $post->fill($request->all()); $post->save(); if ($tags = $request->input('tags')) { $post->retag($request->input('tags')); } return $post->load('tagged'); }
/** * Store a newly created resource in storage. * * @return Response */ public function store() { $rules = ['published_at' => 'date_format:"Y-m-d H:i:s"', 'categories' => 'array|not_inInArray:1|integerInArray|existsInArray:post_category,id', 'status' => 'in:published,draft,trash', 'visibility' => 'in:public,private', 'tags' => 'array|stringInAarray', 'title' => 'min:1|max:100', 'slug' => 'required|alpha_dash|min:1|max:100', 'content' => '']; $validator = Validator::make(Input::only(array_keys($rules)), $rules); if ($validator->fails()) { throw new ResourceException($validator->errors()->first()); } $post = new Post(); $fields = ['slug', 'status', 'visibility', 'published_at']; foreach ($fields as $key => $field) { if (Input::has($field)) { $post->{$field} = Input::get($field); } } //field which can null/empty string $fields = ['title', 'content']; foreach ($fields as $key => $field) { if (Input::get($field) === '') { $post->{$field} = null; } elseif (Input::has($field)) { $post->{$field} = Input::get($field); } } $post->save(); $post->categories()->sync(Input::get('categories', [])); if (Input::has('tags')) { $post->retag(Input::get('tags')); } return $this->show($post->id); }