public function postEdit($id = null) { $check_post_active = Post::whereDelete_status(0)->whereId($id)->first(); if ($check_post_active) { $data = Input::all(); $validator = Post::validate($data); if ($validator->fails()) { return Redirect::to('admin/post/edit/' . $id)->withInput()->withErrors($validator); } else { $check_post_active->title = $data['title']; $check_post_active->content = $data['content']; $check_post_active->category_id = $data['category']; $destination_path = './public/images/post/'; // upload path if (isset($data['remove_image'])) { if ($check_post_active->image) { if (File::exists($destination_path . $check_post_active->image)) { File::delete($destination_path . $check_post_active->image); } } $check_post_active->image = ""; } else { if (Input::file('image')) { $extension = Input::file('image')->getClientOriginalExtension(); // getting image extension $file_name = str_random(8) . '.' . $extension; // renameing image if (Input::file('image')->move($destination_path, $file_name)) { if ($check_post_active->image != "" && File::exists($destination_path . $check_post_active->image)) { File::delete($destination_path . $check_post_active->image); } $check_post_active->image = $file_name; } } } $check_post_active->save(); Session::flash('edit_status', ['status' => 'success', 'message' => 'Edit post is success!']); return redirect()->back(); } } else { return redirect('post/edit'); } }
/** * Function manager posts * @author Tran Van Moi * @since 2015/06/01 * @return response */ public function getPosts() { $data['posts'] = Post::whereDelete_status(0)->join('categories', 'posts.category_id', '=', 'categories.id')->select('posts.id', 'posts.title', 'posts.content', 'posts.image', 'posts.created_at', 'posts.updated_at', 'categories.name as category_name')->orderBy('posts.created_at', 'desc')->orderBy('posts.updated_at', 'desc')->get(); return view('admin/post', $data); }