'tag', ]; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ "blog_title" => [ 'required', function ($attribute, $value, $fail) { // check question is unique in that table $question_exists = Blog::where('blog_title', $value)->where('id', '!=', $this->blog_id)->where('tag_id', request()->input('tag_id'))->count() > 0; if ($question_exists) { $fail('The title must be must be unique for this tag.'); } }, ], "blog_description" => 'required', // "tag_id" => 'required', ]; } public function messages() { return [ 'required' => 'The :attribute field is required.', 'unique' => 'The :attribute field is unique.', ]; } public function validated() { $blog = Blog::find($this->blog_id); $image = basename($blog->blog_image); if ($this->has('blog_image')) { if (\File::exists(public_path('/uploads/blog/images/' . $blog->blog_image . ''))) { \File::delete(public_path('/uploads/blog/images/' . $blog->blog_image . '')); } $image = time() . '.' . $this->blog_image->extension(); $this->blog_image->move(public_path('/uploads/blog/images'), $image); } return array_merge(parent::validated(), [ 'slug' => SlugService::createSlug(Blog::class, 'slug', $this->blog_title), 'blog_image' => $image ]); } }