true, 'showMenu' => true ]; $breadcrumbs = [['link' => '/', 'name' => 'Home'], ['name' => 'Video Category']]; $createUrl = [['url' => 'create', 'name' => "Create", 'slug' => 'video-category']]; $videoCategories = VideoCategory::all(); return view('content.video-categories.index', [ 'breadcrumbs' => $breadcrumbs, 'pageConfigs' => $pageConfigs, 'createUrl' => $createUrl, 'videoCategories' => $videoCategories ]); } public function create() { $pageConfigs = [ 'pageHeader' => true, 'showMenu' => true ]; $breadcrumbs = [['link' => '/admin/video-category', 'name' => 'Video Category'], ['name' => 'Add']]; return view('content.video-categories.create', [ 'pageConfigs' => $pageConfigs, 'breadcrumbs' => $breadcrumbs, ]); } public function store(Request $request) { $request->validate([ 'name' => ['required', 'min: 3'], 'picture' => ['required', 'image'], ]); try { DB::beginTransaction(); $videoCategory = new VideoCategory(); $videoCategory->name = $request->name; $videoCategory->created_by = Auth::user()->role; if ($request->hasFile('picture')) { $prefix = 'video-category'; $categoryImagePath = $this->uploadFile($request->picture, $prefix, false); $videoCategory->image = $categoryImagePath; } $videoCategory->save(); DB::commit(); return redirect()->route('video-category.index')->with([ 'success' => "Video category created successfully" ]); } catch (\Throwable $exception) { DB::rollBack(); return redirect()->back()->with([ 'error' => $exception->getMessage() ]); } } public function edit(VideoCategory $videoCategory) { $pageConfigs = [ 'pageHeader' => true, 'showMenu' => true ]; $breadcrumbs = [['link' => '/admin/video-category', 'name' => 'Video Categories'], ['name' => 'Edit']]; return view('content.video-categories.edit', [ 'pageConfigs' => $pageConfigs, 'breadcrumbs' => $breadcrumbs, 'videoCategory' => $videoCategory ]); } public function update(Request $request, VideoCategory $videoCategory) { $request->validate([ 'name' => ['required', 'min: 3'], 'picture' => ['nullable', 'image'], ]); try { if ($videoCategory->created_by =='ADMIN' && auth()->user()->role != 'ADMIN'){ return redirect()->back()->with([ 'error' => 'You do not have the permission to perform this task' ]); } DB::beginTransaction(); $videoCategory->name = $request->name; $videoCategory->status = $request->status; if ($request->hasFile('picture')) { $prefix = 'video-category'; $categoryImagePath = $this->updateFile($request->picture, $videoCategory->image, $prefix, false); $videoCategory->image = $categoryImagePath; } $videoCategory->update(); DB::commit(); return redirect()->back()->with([ 'success' => "Video Category updated successfully" ]); } catch (\Throwable $exception) { DB::rollBack(); return redirect()->back()->with([ 'error' => $exception->getMessage() ]); } } public function destroy(VideoCategory $videoCategory) { try { if ($videoCategory->created_by =='ADMIN' && auth()->user()->role != 'ADMIN'){ return redirect()->back()->with([ 'error' => 'You do not have the permission to perform this task' ]); } if ($videoCategory->videos->count() > 0){ return redirect()->back()->with([ 'error' => 'Please delete all the records that are related to this category.' ]); } DB::beginTransaction(); $this->deleteFile($videoCategory->image); $videoCategory->delete(); DB::commit(); return redirect()->back()->with([ 'success' => "Video Category deleted successfully" ]); } catch (\Throwable $exception) { DB::rollBack(); return redirect()->back()->with([ 'error' => $exception->getMessage() ]); } } }