Skip to content

Commit

Permalink
Editare categoriei terminata + stergerea categoriei terminata
Browse files Browse the repository at this point in the history
  • Loading branch information
brobert04 committed Jan 7, 2023
1 parent dcb13bd commit cd34981
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 14 deletions.
30 changes: 29 additions & 1 deletion app/Http/Controllers/Admin/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
Expand Down Expand Up @@ -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')){
Expand All @@ -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!');
}
}
3 changes: 1 addition & 2 deletions app/Http/Requests/CreateCategoryRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
Binary file not shown.
13 changes: 5 additions & 8 deletions resources/views/admin/category/categories.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<thead>
<tr>
<th class="text-center">Titlu</th>
<th class="text-center">Subtitlu</th>
<th class="text-center">Vizualizări</th>
<th class="text-center">Fotografie</th>
<th class="text-center">Meta Description & Meta Keywords</th>
Expand All @@ -35,11 +34,9 @@
<tbody>
@foreach($categories as $c)
<tr>
<td>
<td style="font-weight:bold;">
{{$c->title}}
</td>
<td>
{{$c->subtitle}}
<p class="text-warning" style="font-size: 12px;">{{$c->subtitle}}</p>
</td>
<td>
{{$c->views}}
Expand All @@ -51,16 +48,16 @@
<td>
{{$c->meta_description}}
<br>
<span class="badge bg-primary">{{$c->meta_keywords}}</span>
<span id="keywords" class="badge bg-primary">{{$c->meta_keywords}}</span>
<td class="text-center">
<a href="{{route('categories.edit-form',
$c->id)}}" class="butoane text-success" title="Editează categorie"><i class="fa-solid fa-xl fa-pen-to-square"></i></a>
&nbsp;
<form id="delete-form-{{$c->id}}" action="" method="post" style="display:inline-block;">
<form id="delete-form-{{$c->id}}" action="{{route('categories.delete', $c->id)}}" method="post" style="display:inline-block;">
@csrf
@method('DELETE')
</form>
<button class="butoane text-danger" title="Șterge categorie" onclick="if(confirm('Confirmați ștergerea utilizatorului {{$c->name}}?')){
<button class="butoane text-danger" title="Șterge categorie" onclick="if(confirm('Confirmați ștergerea categoriei {{$c->title}}?')){
event.preventDefault();
document.getElementById('delete-form-'+{{$c->id}}).submit();}
"><i class="fa-sharp fa-xl fa-solid fa-trash"></i></button>
Expand Down
18 changes: 15 additions & 3 deletions resources/views/admin/category/edit-category.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<div class="card-body">
<div class="form-group">
<label for="title">Nume categorie</label>
<input name="title" type="text" class="form-control @error('name') is-invalid @enderror" id="name" placeholder="Nume categorie" value="{{old('title') ? old('title') : $category->title}}">
@error('name') <span class="text-danger small">{{$message}}</span>@enderror
<input name="title" type="text" class="form-control @error('title') is-invalid @enderror" id="title" placeholder="Nume categorie" value="{{old('title') ? old('title') : $category->title}}">
@error('title') <span class="text-danger small">{{$message}}</span>@enderror
</div>
<div class="form-group">
<label for="text">Subtitlu</label>
Expand All @@ -22,7 +22,7 @@
</div>
<div class="form-group">
<label for="slug">Slug</label>
<input type="text" name="phone_number" class="form-control @error('phone_number') is-invalid @enderror" id="slug" placeholder="Slug" value="{{old('slug') ? old('slug') : $category->slug}}">
<input type="text" name="slug" class="form-control @error('phone_number') is-invalid @enderror" id="slug" placeholder="Slug" value="{{old('slug') ? old('slug') : $category->slug}}">
@error('slug') <span class="text-danger small">{{$message}}</span>@enderror
</div>
<div class="form-group">
Expand Down Expand Up @@ -123,6 +123,18 @@
more.hidden = !more.hidden;
});
</script>
<script>
$('#title').on('blur',function(){
var theTitle=this.value.toLowerCase().trim(),
slugInput=$('#slug'),
theSlug=theTitle.replace(/&/g,'-and-')
.replace(/[^a-z0-9-]+/g,'-')
.replace(/\-\-+/g,'-')
.replace(/^-+|-+$/g,'');
slugInput.val(theSlug);
});
</script>
@endsection


1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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========
Expand Down

0 comments on commit cd34981

Please sign in to comment.