authorizeResource(Approach::class); } /** * 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 { /** @var \Illuminate\Database\Eloquent\Builder $approachModels */ $approachModels = Approach::query()->where('type', ApproachTypeEnum::make($request->approach_type)); /** @var string $search Search field */ $search = $request->search; if ($search) { $this->searchQuery( $approachModels, $search, null, 'name', ); } $searchFields = \implode(', ', [ trans('validation.attributes.name'), ]); /** Sort columns with a query */ $this->sortQuery($approachModels); /** Custom pagination */ $approachModels = $this->paginate($approachModels); return view('bo.pages.approaches.index', compact('approachModels', 'search', 'searchFields')); } /** * Show the specified resource. * * @param \App\Models\Approach $approach * @return \Illuminate\Contracts\View\View */ public function show(Approach $approach): \Illuminate\Contracts\View\View { return view('bo.pages.approaches.show', ['approachModel' => $approach]); } /** * Show the form for creating a new resource. * * @param \App\Models\Approach $approach * @return \Illuminate\Contracts\View\View */ public function create(Approach $approach): \Illuminate\Contracts\View\View { return view('bo.pages.approaches.create', ['approachModel' => $approach]); } /** * Store a newly created resource in storage. * * @param \App\Http\Requests\Bo\Approaches\StoreApproachRequest $request * @return \Illuminate\Http\RedirectResponse */ public function store(StoreApproachRequest $request): \Illuminate\Http\RedirectResponse { return DB::transaction(function () use ($request) { $approach = new Approach(); $approach->fill($request->validated()); if ($approach->saveOrFail()) { return redirect()->route('bo.approaches.edit', $approach) ->with('success', trans('crud.messages.has_been_created', [ 'model' => Str::ucfirst( trans_choice($approach->type ? 'validation.custom.demarche' : 'validation.custom.label', 1) ) ])); } return redirect()->back() ->with('error', trans('crud.messages.cannot_be_created', [ 'model' => Str::ucfirst( trans_choice($approach->type ? 'validation.custom.demarche' : 'validation.custom.label', 1) ) ])); }); } /** * Show the form for editing the specified resource. * * @param \App\Models\Approach $approach * @return \Illuminate\Contracts\View\View */ public function edit(Approach $approach): \Illuminate\Contracts\View\View { return view('bo.pages.approaches.edit', ['approachModel' => $approach]); } /** * Update the specified resource in storage. * * @param \App\Http\Requests\Bo\Approaches\UpdateApproachRequest $request * @param \App\Models\Approach $approach * @return \Illuminate\Http\RedirectResponse */ public function update(UpdateApproachRequest $request, Approach $approach): \Illuminate\Http\RedirectResponse { return DB::transaction(function () use ($request, $approach) { $approach->fill($request->validated()); if ($approach->saveOrFail()) { return redirect()->route('bo.approaches.edit', $approach) ->with('success', trans('crud.messages.has_been_updated', [ 'model' => Str::ucfirst( trans_choice($approach->type ? 'validation.custom.demarche' : 'validation.custom.label', 1) ) ])); } return redirect()->route('bo.approaches.edit', $approach) ->with('error', trans('crud.messages.cannot_be_updated', [ 'model' => Str::ucfirst( trans_choice($approach->type ? 'validation.custom.demarche' : 'validation.custom.label', 1) ) ])); }); } /** * Remove the specified resource from storage. * * @param \App\Models\Approach $approach * @return \Illuminate\Http\RedirectResponse */ public function destroy(Approach $approach): \Illuminate\Http\RedirectResponse { /** @var array */ $previousQueries = []; \parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries); if ($approach->deleteOrFail()) { return redirect()->route('bo.approaches.index', array_merge( ['approach_type' => $approach->type], $previousQueries ))->with('success', trans('crud.messages.has_been_deleted', [ 'model' => Str::ucfirst( trans_choice($approach->type ? 'validation.custom.demarche' : 'validation.custom.label', 1) ) ])); } return redirect()->back() ->with('error', trans('crud.messages.cannot_be_deleted', [ 'model' => Str::ucfirst( trans_choice($approach->type ? 'validation.custom.demarche' : 'validation.custom.label', 1) ) ])); } /** * Duplicate the specified resource. * * @param \App\Models\Approach $approach * @return \Illuminate\Contracts\View\View */ public function duplicate(Approach $approach): \Illuminate\Contracts\View\View { return $this->create($approach->replicate()); } /** * Get a paginate resource. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function jsonPaginate(Request $request): \Illuminate\Http\JsonResponse { /** @var \App\Models\Approach $approachModels */ $approachModels = $request->targetModel::query() ->where('type', ApproachTypeEnum::demarche) ->where('name', 'like', "%{$request->input('search')}%") ->orderBy('name', 'ASC') ->paginate($request->input('paginate') ?? 5); return \response()->json($approachModels); } }