paginate(20); return view('Staff.article.index', ['posts' => $posts]); } /** * Add A Article * * @access public * @return Staff.article.add */ public function add() { if (Request::isMethod('post')) { $input = Request::all(); $post = new Article(); $post->title = $input['title']; $post->slug = str_slug($post->title); $post->content = $input['content']; $post->user_id = Auth::user()->id; // Verify that an image was upload if (Request::hasFile('image') && Request::file('image')->getError() == 0) { // The file is an image if (in_array(Request::file('image')->getClientOriginalExtension(), ['jpg', 'jpeg', 'bmp', 'png', 'tiff'])) { // Move and add the name to the object that will be saved $post->image = 'article-' . uniqid() . '.' . Request::file('image')->getClientOriginalExtension(); Request::file('image')->move(getcwd() . '/files/img/', $post->image); } else { // Image null or wrong format $post->image = null; } } else { // Error on the image so null $post->image = null; } $v = Validator::make($post->toArray(), $post->rules); if ($v->fails()) { // Delete the image because the validation failed if (file_exists(Request::file('image')->move(getcwd() . '/files/img/' . $post->image))) { unlink(Request::file('image')->move(getcwd() . '/files/img/' . $post->image)); } return Redirect::route('staff_article_index')->with(Toastr::error('Your article has failed to published!', 'Error!', ['options'])); } else { Auth::user()->articles()->save($post); return Redirect::route('staff_article_index')->with(Toastr::success('Your article has successfully published!', 'Yay!', ['options'])); } } return view('Staff.article.add'); } /** * Edit Article * * @access public * @param $slug Slug of article * @param $id Id of article * @return Staff.article.edit */ public function edit($slug, $id) { $post = Article::findOrFail($id); if (Request::isMethod('post')) { $input = Request::all(); $post->title = $input['title']; $post->slug = str_slug($post->title); $post->content = $input['content']; $post->user_id = Auth::user()->id; // Verify that an image was upload if (Request::hasFile('image') && Request::file('image')->getError() == 0) { // The file is an image if (in_array(Request::file('image')->getClientOriginalExtension(), ['jpg', 'jpeg', 'bmp', 'png', 'tiff'])) { // Move and add the name to the object that will be saved $post->image = 'article-' . uniqid() . '.' . Request::file('image')->getClientOriginalExtension(); Request::file('image')->move(getcwd() . '/files/img/', $post->image); } else { // Image null or wrong format $post->image = null; } } else { // Error on the image so null $post->image = null; } $v = Validator::make($post->toArray(), $post->rules); if ($v->fails()) { return Redirect::route('staff_article_index')->with(Toastr::error('Your article changes have failed to publish!', 'Error!', ['options'])); } else { $post->save(); return Redirect::route('staff_article_index')->with(Toastr::success('Your article changes have successfully published!', 'Yay!', ['options'])); } } return view('Staff.article.edit', ['post' => $post]); } /** * Delete the article * * @access public * @param $slug Slug of article * @param $id Id of article * @return void */ public function delete($slug, $id) { $post = Article::findOrFail($id); $post->delete(); return Redirect::route('staff_article_index')->with(Toastr::info('Article has successfully been deleted', 'Info!', ['options'])); } }