[0924b6c] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | namespace App\Http\Controllers\Dashboard;
|
---|
| 4 |
|
---|
| 5 | use App\Helpers\Alert;
|
---|
| 6 | use App\Http\Requests\Dashboard\CategoryRequest;
|
---|
| 7 | use App\Models\Post;
|
---|
| 8 | use App\Models\Category;
|
---|
| 9 | use Illuminate\Http\Request;
|
---|
| 10 | use App\Http\Controllers\Controller;
|
---|
| 11 | use Illuminate\Support\Facades\Storage;
|
---|
| 12 |
|
---|
| 13 | class CategoriesController extends Controller
|
---|
| 14 | {
|
---|
| 15 | public function index()
|
---|
| 16 | {
|
---|
| 17 | return view("dashboard.categories.index")->with([
|
---|
| 18 | "categories" => Category::all()
|
---|
| 19 | ]);
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | public function create()
|
---|
| 23 | {
|
---|
| 24 | return view("dashboard.categories.create");
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | public function editShow($id)
|
---|
| 28 | {
|
---|
| 29 | return view("dashboard.categories.edit")->with([
|
---|
| 30 | "category" => Category::findOrFail($id)
|
---|
| 31 | ]);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | public function edit(CategoryRequest $request, $id)
|
---|
| 35 | {
|
---|
| 36 | $category = Category::findOrFail($id);
|
---|
| 37 |
|
---|
| 38 | $category->name = $request->name;
|
---|
| 39 | $category->color = $request->color;
|
---|
| 40 |
|
---|
| 41 | $category->save();
|
---|
| 42 |
|
---|
| 43 | Alert::flash("Category edited successfully");
|
---|
| 44 |
|
---|
| 45 | return redirect()->route("dashboard.categories.index");
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public function store(CategoryRequest $request)
|
---|
| 49 | {
|
---|
| 50 | $category = new Category();
|
---|
| 51 |
|
---|
| 52 | $category->name = $request->name;
|
---|
| 53 | $category->color = $request->color;
|
---|
| 54 |
|
---|
| 55 | $category->save();
|
---|
| 56 |
|
---|
| 57 | Alert::flash("New category added successfully");
|
---|
| 58 |
|
---|
| 59 | return redirect()->route("dashboard.categories.create");
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public function block(Request $request, $id)
|
---|
| 63 | {
|
---|
| 64 | $category = Category::find($id);
|
---|
| 65 | $posts = Post::where("category_id", $id)->get();
|
---|
| 66 |
|
---|
| 67 | foreach ($posts as $post) {
|
---|
| 68 | $post->is_active = false;
|
---|
| 69 | $post->save();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | $category->is_active = false;
|
---|
| 73 | $category->save();
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | Alert::flash($category->name . " blocked successfully");
|
---|
| 77 |
|
---|
| 78 | return redirect()->route("dashboard.categories.index");
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | public function unblock(Request $request, $id)
|
---|
| 82 | {
|
---|
| 83 | $category = Category::find($id);
|
---|
| 84 | $posts = Post::where("category_id", $id)->get();
|
---|
| 85 |
|
---|
| 86 | foreach ($posts as $post) {
|
---|
| 87 | $post->is_active = true;
|
---|
| 88 | $post->save();
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | $category->is_active = true;
|
---|
| 92 | $category->save();
|
---|
| 93 |
|
---|
| 94 | Alert::flash($category->name . " unblocked successfully");
|
---|
| 95 |
|
---|
| 96 | return redirect()->route("dashboard.categories.index");
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | public function destroy(Request $request, $id)
|
---|
| 100 | {
|
---|
| 101 | $category = Category::find($id);
|
---|
| 102 |
|
---|
| 103 | if (Post::where("category_id", $id)->count() > 0) {
|
---|
| 104 |
|
---|
| 105 | $posts = Post::where("category_id", $id)->get();
|
---|
| 106 |
|
---|
| 107 | foreach ($posts as $post) {
|
---|
| 108 | $post->category()->dissociate($category);
|
---|
| 109 | Storage::disk('uploads')->delete($post->image_link);
|
---|
| 110 | $post->delete();
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | $category->delete();
|
---|
| 115 |
|
---|
| 116 | Alert::flash($category->name . " deleted successfully");
|
---|
| 117 |
|
---|
| 118 | return redirect()->route("dashboard.categories.index");
|
---|
| 119 | }
|
---|
| 120 | }
|
---|