authorizeResource(CategoryApproaches::class, 'categories_approach'); } /** * Display a listing of the resource. * @param \Illuminate\Http\Request $request * @return \Illuminate\Contracts\View\View */ public function index(Request $request): \Illuminate\Contracts\View\View { $categories = CategoryApproaches::query(); $count = CategoryApproaches::count(); /** @var string $search Search field */ $search = $request->search; if ($search) { $this->searchQuery( $categories, $search, null, 'name', ); } $searchFields = \implode(', ', [ trans('validation.attributes.name'), ]); /** Sort columns with a query */ $this->sortQuery($categories); $categories = $categories->getModels(); return view('bo.pages.categories_approaches.index', compact('categories', 'count', 'searchFields', 'search')); } /** * Show the form for creating a new resource. * * @param \App\Models\CategoryApproaches $categoryApproaches * @return \Illuminate\Contracts\View\View */ public function create(CategoryApproaches $categoryApproaches) { $count = CategoryApproaches::count(); if ($count >= CategoryApproaches::MAX_CATEGORIES) { return redirect() ->route('bo.categories.approaches.index') ->with('error', "Limite atteinte : vous ne pouvez pas créer plus de ".CategoryApproaches::MAX_CATEGORIES." catégories."); } return view('bo.pages.categories_approaches.create', ['categoryApproachesModel' =>$categoryApproaches]); } /** * Store a newly created resource in storage. * * @param \App\Http\Requests\Bo\CategoryApproachRequest $request * @return \Illuminate\Http\RedirectResponse */ public function store(CategoryApproachRequest $request): \Illuminate\Http\RedirectResponse { if (CategoryApproaches::count() >= CategoryApproaches::MAX_CATEGORIES) { throw ValidationException::withMessages([ 'name' => "Limite atteinte : maximum ".CategoryApproaches::MAX_CATEGORIES." catégories." ]); } return DB::transaction(function () use ($request) { $category = new CategoryApproaches(); $category->fill($request->validated()); if($category->saveOrFail()) { return redirect()->route('bo.categories.approaches.index') ->with('success', trans('crud.messages.has_been_created', [ 'model' => Str::ucfirst(trans('validation.custom.category')) ])); } }); return redirect()->back() ->with('error', trans('crud.messages.cannot_be_created', [ 'model' => Str::ucfirst(trans('validation.custom.custom')) ])); } /** * Show the form for editing the specified resource. * * @param \App\Models\CategoryApproaches $categories_approach * @return \Illuminate\Contracts\View\View */ public function edit(CategoryApproaches $categories_approach ): \Illuminate\Contracts\View\View { return view('bo.pages.categories_approaches.edit', [ 'categoryApproachesModel' => $categories_approach ]); } /** * Show the form for editing the specified resource. * * @param \App\Models\CategoryApproaches $categories_approach * @return \Illuminate\Contracts\View\View */ public function show(CategoryApproaches $categories_approach ): \Illuminate\Contracts\View\View { return view('bo.pages.categories_approaches.show', [ 'categoryApproachesModel' => $categories_approach ]); } /** * Update the specified resource in storage. * * @param \App\Http\Request\Bo\CategoryApproachRequest $request * @param \App\Models\CategoryApproaches $categories_approach * @return \Illuminate\Http\RedirectResponse */ public function update(CategoryApproachRequest $request, CategoryApproaches $categories_approach ) { if ($categories_approach ->update($request->validated())) { return redirect() ->route('bo.categories.approaches.index') ->with('success', trans('crud.messages.has_been_updated', [ 'model' => Str::ucfirst('models.classes.categoryApproach')] )); } return redirect()->route('bo.categories.approaches.edit', $categories_approach ) ->with('error', trans('crud.messages.cannot_be_updated', [ 'model' => Str::ucfirst('models.classes.categoryApproach') ])); } /** * Remove the specified resource from storage. * * @param \App\Models\CategoryApproaches $categories_approach * @return \Illuminate\Http\RedirectResponse */ public function destroy(CategoryApproaches $categories_approach ): \Illuminate\Http\RedirectResponse { if (CategoryApproaches::count() === CategoryApproaches::MIN_CATEGORIES) { return redirect() ->route('bo.categories.approaches.index') ->with('error', "Limite atteinte : vous ne pouvez pas supprimer tous les catégories (une catégorie minimum)."); } if ($categories_approach ->delete()) { return redirect() ->route('bo.categories.approaches.index') ->with('success', trans('crud.messages.has_been_deleted', [ 'model' => Str::ucfirst(trans_choice('models.classes.categoryApproach', 1))])); } return redirect()->back() ->with('success', trans('crud.messages.cannot_be_deleted', [ 'model' => Str::ucfirst(trans_choice('models.classes.categoryApproach', 1))])); } /** * Remove the specified resource from storage. * @param CategoryApproaches $section_approach * @return \Illuminate\Contracts\View\View */ public function duplicate(CategoryApproaches $categories_approach ) { return $this->create($categories_approach ->replicate()); } /** * Get a paginate resource. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function jsonPaginateCategory(Request $request): \Illuminate\Http\JsonResponse { $categoryApproachModel =$request->targetModel::query()->orderBy('name', 'ASC')->paginate(); return \response()->json($categoryApproachModel); } }