diff --git a/app/Http/Controllers/Admin/CategoryController.php b/app/Http/Controllers/Admin/CategoryController.php index 6eac3df..9b4a6f0 100644 --- a/app/Http/Controllers/Admin/CategoryController.php +++ b/app/Http/Controllers/Admin/CategoryController.php @@ -8,6 +8,7 @@ use App\Http\Requests\CreateCategoryRequest; use Illuminate\Support\Facades\File; use Illuminate\Support\Str; +use App\Http\Requests\UpdateCategoryRequest; class CategoryController extends Controller { @@ -21,6 +22,14 @@ public function newCategoryForm(){ } public function createCategory(CreateCategoryRequest $request){ + $this->validate($request, + [ + 'slug'=>'unique:categories,slug' + ], + [ + 'slug.unique'=>'Slug-ul este deja folosit de o altă categorie!' + ] + ); $category = new Category(); $category->title = $request->title; $category->subtitle = $request->subtitle; @@ -49,7 +58,17 @@ public function editCategoryForm($id){ return view('admin.category.edit-category')->with('category', $category); } - public function editCategory(Request $request, $id){ + public function editCategory(CreateCategoryRequest $request, $id){ + + $this->validate($request, + [ + 'slug'=>'unique:categories,slug,'.$id, + ], + [ + 'slug.unique'=>'Slug-ul este deja folosit de o altă categorie!' + ] + ); + $category = Category::find($id); if($request->hasFile('photo')){ @@ -72,4 +91,13 @@ public function editCategory(Request $request, $id){ $category->save(); return redirect(route('categories'))->withInput()->with('success', 'Categoria' .' '.$category->title. ' '. 'a fost editată cu succes!'); } + + public function deleteCategory($id){ + $category = Category::find($id); + if(!($category->photo == 'category.jpg')){ + File::delete('images/categories/'.$category->photo); + } + $category->delete(); + return redirect(route('categories'))->withInput()->with('success', 'Categoria' .' '.$category->title. ' '. 'a fost ștearsă cu succes!'); + } } diff --git a/app/Http/Requests/CreateCategoryRequest.php b/app/Http/Requests/CreateCategoryRequest.php index d4ac250..89ce949 100644 --- a/app/Http/Requests/CreateCategoryRequest.php +++ b/app/Http/Requests/CreateCategoryRequest.php @@ -26,7 +26,7 @@ public function rules() return [ 'title' => 'required|max:50', 'subtitle' => 'max:255', - 'slug' => 'required|max:255|unique:categories', + 'slug' => 'required|max:255', 'excerpt' => 'max:6000', 'photo' => 'max:2048', 'meta_title' => 'max:255', @@ -42,7 +42,6 @@ public function messages(){ 'subtitle.max' => 'Subtitlul nu poate avea mai mult de 255 de caractere', 'slug.required' => 'Slug-ul este obligatoriu', 'slug.max' => 'Slug-ul nu poate avea mai mult de 255 de caractere', - 'slug.unique' => 'Slug-ul trebuie să fie unic', 'excerpt.max' => 'Excerpt-ul nu poate avea mai mult de 6000 de caractere', 'photo.max' => 'Imaginea nu poate avea mai mult de 2MB', 'meta_title.max' => 'Meta titlul nu poate avea mai mult de 255 de caractere', diff --git a/public/images/categories/CartideFilozofie_1672433196.jpg b/public/images/categories/CartideFilozofie_1672433196.jpg deleted file mode 100644 index fe7486a..0000000 Binary files a/public/images/categories/CartideFilozofie_1672433196.jpg and /dev/null differ diff --git a/resources/views/admin/category/categories.blade.php b/resources/views/admin/category/categories.blade.php index 1c7084a..aafe331 100644 --- a/resources/views/admin/category/categories.blade.php +++ b/resources/views/admin/category/categories.blade.php @@ -25,7 +25,6 @@ Titlu - Subtitlu Vizualizări Fotografie Meta Description & Meta Keywords @@ -35,11 +34,9 @@ @foreach($categories as $c) - + {{$c->title}} - - - {{$c->subtitle}} +

{{$c->subtitle}}

{{$c->views}} @@ -51,16 +48,16 @@ {{$c->meta_description}}
- {{$c->meta_keywords}} + {{$c->meta_keywords}}   -
+ @csrf @method('DELETE')
- diff --git a/resources/views/admin/category/edit-category.blade.php b/resources/views/admin/category/edit-category.blade.php index bdfe655..a4ade34 100644 --- a/resources/views/admin/category/edit-category.blade.php +++ b/resources/views/admin/category/edit-category.blade.php @@ -12,8 +12,8 @@
- - @error('name') {{$message}}@enderror + + @error('title') {{$message}}@enderror
@@ -22,7 +22,7 @@
- + @error('slug') {{$message}}@enderror
@@ -123,6 +123,18 @@ more.hidden = !more.hidden; }); + @endsection diff --git a/routes/web.php b/routes/web.php index 7aff1c7..d3f56cf 100644 --- a/routes/web.php +++ b/routes/web.php @@ -43,6 +43,7 @@ Route::post('categories/new', [\App\Http\Controllers\Admin\CategoryController::class,'createCategory'])->name('categories.create'); Route::get('categories/edit/{id}', [\App\Http\Controllers\Admin\CategoryController::class,'editCategoryForm'])->name('categories.edit-form'); Route::put('categories/edit/{id}', [\App\Http\Controllers\Admin\CategoryController::class,'editCategory'])->name('categories.edit'); + Route::delete('categories/delete/{id}', [\App\Http\Controllers\Admin\CategoryController::class,'deleteCategory'])->name('categories.delete'); }); //======Rutele utilizatorului========