postRepository->filter($request->merge(['page' => $request->get('page') ?? 1])->all()); return view('administrator.pages.post.index', compact('title', 'posts')); } public function create() { $title = 'Add new Post'; return view('administrator.pages.post.add', compact('title')); } public function store(PostRequest $request) { $request->validated(); $isExits = $this->postRepository->findBy('slug', $request->get('slug')); if ($isExits) { return back()->withErrors('The Slug already exits'); } DB::beginTransaction(); try { $this->postRepository->create($request->all()); DB::commit(); } catch (\Exception $error) { DB::rollBack(); return back()->withErrors($error->getMessage()); } return redirect()->route('post.index')->with('success', 'Added successfully'); } public function edit(int $id = 0) { $post = $this->postRepository->find($id); if (!$post) { return back()->withErrors('Invalid request'); } $title = 'Edit Post'; return view('administrator.pages.post.edit', compact('title', 'post')); } public function update(int $id = 0,PostRequest $request) { $post = $this->postRepository->find($id); if (!$post) { return back()->withErrors('Invalid request'); } $request->validated(); if($request->slug !== $post->slug) { $isExits = $this->postRepository->findBy('slug', $request->get('slug')); if ($isExits) { return back()->withErrors('The Slug already exits'); } } DB::beginTransaction(); try { $this->postRepository->update($id,$request->all()); DB::commit(); } catch (\Exception $error) { DB::rollBack(); return back()->withErrors($error->getMessage()); } return redirect()->route('post.index')->with('success', 'Updated successfully'); } public function destroy(int $id = 0) { $post = $this->postRepository->find($id); if (!$post) { return back()->withErrors('Invalid request'); } DB::beginTransaction(); try { $this->postRepository->delete($id); DB::commit(); } catch (\Exception $error) { DB::rollBack(); return back()->withErrors($error->getMessage()); } return redirect()->route('post.index')->with('success', 'Deleted successfully'); } }