92 lines
No EOL
2.7 KiB
PHP
92 lines
No EOL
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\CategoryApproaches;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class CategoryApproachesController extends Controller
|
|
{
|
|
private const MAX_CATEGORIES = 6;
|
|
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(CategoryApproaches::class);
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$categories = CategoryApproaches::query();
|
|
$count = CategoryApproaches::count();
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($categories);
|
|
$categories = $categories->getModels();
|
|
return view('bo.pages.categories_approaches.index', compact('categories', 'count'));
|
|
}
|
|
|
|
public function create(CategoryApproaches $categoryApproaches)
|
|
{
|
|
$count = CategoryApproaches::count();
|
|
|
|
if ($count >= self::MAX_CATEGORIES) {
|
|
return redirect()
|
|
->route('bo.categories-approaches.index')
|
|
->with('error', "Limite atteinte : vous ne pouvez pas créer plus de ".self::MAX_CATEGORIES." catégories.");
|
|
}
|
|
|
|
return view('bo.pages.categories_approaches.create', ['categoryApproachesModel' =>$categoryApproaches]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
if (CategoryApproaches::count() >= self::MAX_CATEGORIES) {
|
|
throw ValidationException::withMessages([
|
|
'title' => "Limite atteinte : maximum ".self::MAX_CATEGORIES." catégories."
|
|
]);
|
|
}
|
|
$data = $request->validate([
|
|
'title' => ['required', 'string', 'between:3,50'],
|
|
]);
|
|
|
|
CategoryApproaches::create($data);
|
|
|
|
return redirect()
|
|
->route('bo.pages.categories-approaches.index')
|
|
->with('success', "Catégorie créée.");
|
|
}
|
|
|
|
public function edit(CategoryApproaches $categories_approach)
|
|
{
|
|
return view('bo.pages.categories_approaches.edit', [
|
|
'category' => $categories_approach
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, CategoryApproaches $categories_approach)
|
|
{
|
|
$data = $request->validate([
|
|
'title' => ['required', 'string', 'between:3,50'],
|
|
]);
|
|
|
|
$categories_approach->update($data);
|
|
|
|
return redirect()
|
|
->route('bo.pages.categories-approaches.index')
|
|
->with('success', "Catégorie mise à jour.");
|
|
}
|
|
|
|
public function destroy(CategoryApproaches $categories_approach)
|
|
{
|
|
$categories_approach->delete();
|
|
|
|
return redirect()
|
|
->route('bo.pages.categories-approaches.index')
|
|
->with('success', "Catégorie supprimée.");
|
|
}
|
|
} |