create($create_data); $this->uploadImages($data, $blog, 'src'); $this->uploadImages($data, $blog, 'm_src'); Event::dispatch('admin.blogs.create.after', $blog); return true; } /** * Update item. * * @param array $data * @param int $id * @return bool */ public function updateItem(array $data, $id) { Event::dispatch('admin.blogs.update.before', $id); $update_data = $data; if ( array_key_exists('src', $update_data) ) { unset($update_data['src']); } if ( array_key_exists('m_src', $update_data) ) { unset($update_data['m_src']); } $blog = $this->update($update_data, $id); $this->uploadImages($data, $blog, 'src'); $this->uploadImages($data, $blog, 'm_src'); Event::dispatch('admin.blogs.update.after', $blog); return true; } /** * Upload category's images. * * @param array $data * @param \Webkul\Category\Contracts\Category $category * @param string $type * @return void */ public function uploadImages($data, $blog, $type = 'src') { $imageUploadService = app(ImageUploadService::class); if (isset($data[$type])) { foreach ($data[$type] as $imageId => $image) { $file = $type . '.' . $imageId; if (request()->hasFile($file)) { if ($blog->{$type}) { $imageUploadService->delete($blog->{$type}); } $result = $imageUploadService->upload( request()->file($file) ); if (! ($result['success'] ?? false)) { throw new \Exception($result['error'] ?? 'Blog image upload failed.'); } $blog->{$type} = $result['path']; $blog->save(); } } } else { if ($blog->{$type}) { $imageUploadService->delete($blog->{$type}); } $blog->{$type} = null; $blog->save(); } } /** * Delete a blog item and delete the image from the disk or where ever it is. * * @param int $id * @return bool */ public function destroy($id) { $blogItem = $this->find($id); $imageUploadService = app(ImageUploadService::class); if ($blogItem->src) { $imageUploadService->delete($blogItem->src); } if ($blogItem->m_src) { $imageUploadService->delete($blogItem->m_src); } return $this->model->destroy($id); } /** * Get only active blogs. * * @return array */ public function getActiveBlogs() { $locale = config('app.locale'); $blogs = DB::table('blogs') ->where('published_at', '<=', Carbon::now()->format('Y-m-d')) ->where('status', 1) ->where('locale', $locale) ->orderBy('id', 'DESC') ->paginate(12); return $blogs; } /** * Get only single blogs. * * @return array */ public function getSingleBlogs($id) { $blog = DB::table('blogs') ->whereSlug($id) ->where('published_at', '<=', Carbon::now()->format('Y-m-d')) ->where('status', 1) ->first(); return $blog; } /** * Get only single blogs. * * @return array */ public function getBlogCategories($id) { $locale = config('app.locale'); $categoryId = DB::table('blog_categories') ->where('slug', $id)->first(); $blogs = DB::table('blogs') ->where('published_at', '<=', Carbon::now()->format('Y-m-d')) ->where('default_category', $categoryId['id']) ->where('status', 1) ->where('locale', $locale) ->orderBy('id', 'DESC') ->paginate(12); return $blogs; } }