public function postNew(DiaryCreateRequest $req) { $img = Image::make($req->file('featured_image')); $ext = $req->file('featured_image')->getClientOriginalExtension(); $fileName = str_slug($req->input('title')) . uniqid() . '.' . $ext; $img->resize(760, null, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); }); if ($img->save(public_path() . '/media/diary/' . $fileName)) { $diary = new Diary(); $diary->title = $req->input('title'); $diary->note = $req->input('content'); $diary->category_id = $req->input('category'); $diary->featured_image = $fileName; $diary->status = $req->input('publish') ? $req->input('publish') : 0; $diary->user_id = Auth::user()->id; if ($diary->save()) { if ($req->input('tags') != '') { $tags = explode(',', $req->input('tags')); $diary->tags()->sync($tags); } return redirect()->back()->with('msg', 'ok'); } } }
/** * Creates a new Diary model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Diary(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->diary_id]); } else { return $this->render('create', ['model' => $model]); } }