public function actionNew() { if ($this->isPost()) { $article = new Article(); try { $article->fill($_POST); $article->save(); $this->redirect('/admin'); } catch (MultiException $error) { $this->view->article = $article; $this->view->error = $error; } } else { $this->view->error = false; } $this->view->display(__DIR__ . '/../../templates/new.php'); }
/** * store a resource * @param Request $request http request * @param mixed $id id of the resource for updating * @return jsend jsend with newly stored source */ function store(Request $request, $id = null) { //////////////// // Load Data // //////////////// if ($id) { $data = Model::find($id); if (!$data) { return app()->abort(404); } } else { $data = new Model(); } /////////////////////////////////// // Assign posted data to Data // /////////////////////////////////// $data->fill($request->input()); /////////////////////////////////////////////////////////////////// // Validate data // /////////////////////////////////////////////////////////////////// # Validate User if ($request->input('user_id')) { if (!is_scalar($request->input('user_id'))) { return response()->json(JSend::fail(['user' => ['Invalid User']])->asArray()); } else { $user = User::find($request->input('user_id')); if (!$user) { return response()->json(JSend::fail(['user' => ['Invalid User']])->asArray()); } $data->user_id = $request->input('user_id'); } } /////////////////////////// // Embeds Other Document // /////////////////////////// /////////////////////// // EMBED IMAGES // /////////////////////// foreach ($this->request->input('images') as $x) { $images[] = new Image($x); } if (!$data->syncImages($images)) { return response()->json(JSend::fail($data->getErrors())->asArray())->setCallback($this->request->input('callback')); } /////////////////////// // EMBED TAGS // /////////////////////// foreach ($this->request->input('tags') as $x) { $tags[] = new Tag($x); } if (!$data->syncTags($tags)) { return response()->json(JSend::fail($data->getErrors())->asArray())->setCallback($this->request->input('callback')); } /////////// // Store // /////////// if ($data->save()) { return response()->json(JSend::success(['data' => $data])->asArray()); } else { return response()->json(JSend::fail($data->getErrors())->asArray()); } }
/** * Stores an article * @return Response */ public function storeArticle() { $data = Input::all(); $article = new Article(); if ($article->validate($data)) { $file = Input::file('featured'); if ($file != null && $file->isValid()) { $data['featured_img'] = 'articles/' . $article->id . '/' . time() . $file->getClientOriginalName(); $file->move(public_path('files/articles/' . $article->id), $data['featured_img']); } $user = Auth::user(); $article->fill($data); $article->user_id = $user->id; $article->save(); Alert::success('Article created ' . ($article->published ? 'and published' : '') . ' successfully. You may go to menus to link this article to a menu', 'Success'); return Redirect::to('admin/articles')->withSuccess('<strong>Article created successfully.</strong>'); } Input::flash(); return View::make('admin.articles.create')->withErrors($article->getValidator()); }