public function postDelete(Request $request) { $this->validate($request, ["id" => "required|numeric"]); $file_id = $request->input('id'); $file = \Coder\Models\File::find($file_id); if ($file) { $posts = $file->posts()->get(); foreach ($posts as $post) { $post->files()->detach(['file_id' => $file->id]); } $dir = public_path($file->directory . $file->file_name); $dir_thumb = public_path($file->directory . $file->thumbnail); File::delete($dir, $dir_thumb); Auth::user()->files()->find($file_id)->delete(); $file->delete(); } }
public function postEdit(\Illuminate\Http\Request $request, $post_id) { $validator = Validator::make(['post_id' => $post_id], ['post_id' => 'required|numeric']); if ($validator->fails()) { abort(404); } $post = Post::find($post_id); if (!$post) { abort(404); } $this->validate($request, ["title" => "required|min:5|max:255", "title_url" => "required|alpha_dash|max:255|unique:posts,title_url," . $post->id, "description" => "min:5|max:1000", "keywords" => "max:255", "body" => "required|min:100", "image" => "image|max:300", "file_id" => "required|numeric"]); $skills = (array) explode(",", $request->input('skills')); $skills = array_filter(array_unique($skills)); $validator = Validator::make(['skills' => count($skills)], ["skills" => "max:5"]); if ($validator->fails()) { return redirect()->back(); } $arrayUpdate = ['title' => $request->input('title'), 'title_url' => $request->input('title_url'), 'description' => $request->input('description'), 'keywords' => $request->input('keywords'), 'body' => $request->input('body')]; if ($request->input('active')) { if (!$post->active) { $arrayUpdate['active'] = true; $arrayUpdate['active_at'] = Carbon::now()->toDateTimeString(); $arrayUpdate['active_by'] = Auth::user()->id; } } else { $arrayUpdate['active'] = false; $arrayUpdate['active_at'] = null; $arrayUpdate['active_by'] = null; } $post->update($arrayUpdate); $post->files()->detach(); if ($request->input('file_id')) { $this_file = File::find($request->input('file_id')); if ($this_file) { $post->files()->attach(['file_id' => $this_file->id]); } } $post->skills()->detach(); foreach ($skills as $value) { $skill = Skill::find($value); if ($skill) { $post->skills()->attach(['skill_id' => $value]); } } notify()->flash("Updated", 'success', ['text' => 'Your post is update.']); return redirect()->route('post.view', ['title_url' => $post->title_url]); }