Initial commit with contrats and domaines modules
This commit is contained in:
63
app/Http/Controllers/CategorieController.php
Normal file
63
app/Http/Controllers/CategorieController.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Categorie;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
|
||||
class CategorieController extends Controller
|
||||
{
|
||||
public function index(): Response
|
||||
{
|
||||
return Inertia::render('Categories/Index', [
|
||||
'categories' => Categorie::withCount('articles')->orderBy('ordre')->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$this->authorize('create', Categorie::class);
|
||||
|
||||
$validated = $request->validate([
|
||||
'nom' => 'required|string|max:100|unique:categories,nom',
|
||||
'description' => 'nullable|string',
|
||||
'couleur' => 'nullable|string|max:7',
|
||||
'icone' => 'nullable|string|max:50',
|
||||
'ordre' => 'nullable|integer|min:0',
|
||||
]);
|
||||
|
||||
Categorie::create($validated);
|
||||
|
||||
return back()->with('success', 'Catégorie créée.');
|
||||
}
|
||||
|
||||
public function update(Request $request, Categorie $categorie): RedirectResponse
|
||||
{
|
||||
$this->authorize('update', Categorie::class);
|
||||
|
||||
$validated = $request->validate([
|
||||
'nom' => 'required|string|max:100|unique:categories,nom,' . $categorie->id,
|
||||
'description' => 'nullable|string',
|
||||
'couleur' => 'nullable|string|max:7',
|
||||
'icone' => 'nullable|string|max:50',
|
||||
'ordre' => 'nullable|integer|min:0',
|
||||
'active' => 'boolean',
|
||||
]);
|
||||
|
||||
$categorie->update($validated);
|
||||
|
||||
return back()->with('success', 'Catégorie mise à jour.');
|
||||
}
|
||||
|
||||
public function destroy(Categorie $categorie): RedirectResponse
|
||||
{
|
||||
$this->authorize('delete', Categorie::class);
|
||||
|
||||
$categorie->update(['active' => false]);
|
||||
|
||||
return back()->with('success', 'Catégorie désactivée.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user