/** * Creates a new PostTag model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new PostTag(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'post_id' => $model->post_id, 'tag_id' => $model->tag_id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Create post * * @param Request $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View */ public function create(Request $request) { if ($request->isMethod("post")) { $rules = ["title" => "required", "alias" => "required|unique:posts,alias", "content" => "required", 'image' => 'mimes:jpeg,jpg,png']; Validator::make($request->all(), $rules)->validate(); $post = new Post(); $post->title = $request->input("title"); $post->alias = $request->input("alias"); $post->content = $request->input("content"); $post->meta_keys = $request->input("meta_keys"); $post->meta_desc = $request->input("meta_desc"); if (!empty($request->file("image"))) { $generated_string = str_random(32); $file = $request->file("image")->store('uploads'); $new_file = $generated_string . '.' . $request->file("image")->getClientOriginalExtension(); Storage::move($file, 'uploads/' . $new_file); $img = Image::make($request->file('image')); $img->crop(200, 200); $img->save(storage_path('app/public/uploads/' . $new_file)); $img = Image::make($request->file('image')); $img->resize(600, 315); $img->save(storage_path('app/public/uploads/fb-' . $new_file)); $post->image = $new_file; } if ($request->has("category")) { $post->category_id = $request->input("category"); } $post->publish = $request->has("publish"); $post->author_id = Auth::user()->id; $post->save(); if ($request->has("tags")) { foreach ($request->input("tags") as $tag) { $post_tag = new PostTag(); $post_tag->post_id = $post->id; $post_tag->tag_id = $tag; $post_tag->save(); } } $admins = User::getAdmins(); foreach ($admins as $admin) { if ($admin->id != Auth::user()->id) { $notification = new Notification(); $notification->from = Auth::user()->id; $notification->to = $admin->id; $notification->type = 3; $notification->save(); } } return redirect()->route('posts'); } else { $categories = Category::getCategoriesByPublish(); $tags = Tag::getTags(); return view("site.post.create", compact("categories", "tags", "post")); } }
private function _setTags($tags_str, $post_id) { PostTag::where('post_id', $post_id)->delete(); $tags = explode(', ', $tags_str); foreach ($tags as $tag) { if (trim($tag) == '') { continue; } $tag = mb_strtolower($tag); $dbtag = Tags::where('tag', 'like', $tag)->first(); if (empty($dbtag)) { $dbtag = new Tags(); $dbtag->tag = strip_tags($tag); $dbtag->save(); } $post_tag = new PostTag(); $post_tag->post_id = $post_id; $post_tag->tag_id = $dbtag->id; $post_tag->save(); } }