authorizeResource(Subrange::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 $subrangeModels */ $subrangeModels = Subrange::query()->with('range'); /** @var string $search Search field */ $search = $request->search; if ($search) { $this->searchQuery( $subrangeModels, $search, null, 'label', 'subtitle', 'description', ); } $searchFields = Str::ucFirst(\implode(', ', [ trans('validation.custom.label'), trans('validation.custom.subtitle'), trans('validation.attributes.description'), ])); /** Sort columns with a query */ $this->sortQuery($subrangeModels); /** Custom pagination */ $subrangeModels = $this->paginate($subrangeModels); return view('bo.pages.subranges.index', compact('subrangeModels', 'search', 'searchFields')); } /** * Show a specific resource. * * @param \App\Models\Subrange $subrange * @return \Illuminate\Contracts\View\View */ public function show(Subrange $subrange): \Illuminate\Contracts\View\View { return view('bo.pages.subranges.show', ['subrangeModel' => $subrange]); } /** * Show the form for creating a new resource. * * @param \App\Models\Subrange $subrange * @return \Illuminate\Contracts\View\View */ public function create(Subrange $subrange): \Illuminate\Contracts\View\View { $rangeModels = Range::query()->orderBy('order')->get(); return view('bo.pages.subranges.create', [ 'subrangeModel' => $subrange, 'rangeModels' => $rangeModels, ]); } /** * Store a newly created resource in storage. * * @param \App\Http\Requests\Bo\Subranges\StoreSubrangeRequest $request * @return \Illuminate\Http\RedirectResponse */ public function store(StoreSubrangeRequest $request): \Illuminate\Http\RedirectResponse { return DB::transaction(function () use ($request) { $subrange = new Subrange(); $subrange->fill($request->validated()); if ($subrange->saveOrFail()) { return redirect()->route('bo.subranges.edit', $subrange) ->with('success', trans('crud.messages.has_been_created', [ 'model' => Str::ucFirst(trans('models.classes.subrange')) ])); } return redirect()->back() ->with('error', trans('crud.messages.cannot_be_created', [ 'model' => Str::ucFirst(trans('models.classes.subrange')) ])); }); } /** * Show the form for editing the specified resource. * * @param \App\Models\Subrange $subrange * @return \Illuminate\Contracts\View\View */ public function edit(Subrange $subrange): \Illuminate\Contracts\View\View { $rangeModels = Range::query()->orderBy('order')->get(); return view('bo.pages.subranges.edit', [ 'subrangeModel' => $subrange, 'rangeModels' => $rangeModels, ]); } /** * Update the specified resource in storage. * * @param \App\Http\Requests\Bo\Subranges\UpdateSubrangeRequest $request * @param \App\Models\Subrange $subrange * @return \Illuminate\Http\RedirectResponse */ public function update( UpdateSubrangeRequest $request, Subrange $subrange ): \Illuminate\Http\RedirectResponse { return DB::transaction(function () use ($request, $subrange) { $subrange->fill($request->validated()); if ($subrange->saveOrFail()) { return redirect()->route('bo.subranges.edit', $subrange) ->with('success', trans('crud.messages.has_been_updated', [ 'model' => Str::ucFirst(trans('models.classes.subrange')) ])); } return redirect()->route('bo.subranges.edit', $subrange) ->with('error', trans('crud.messages.cannot_be_updated', [ 'model' => Str::ucFirst(trans('models.classes.subrange')) ])); }); } /** * Remove the specified resource from storage. * * @param \App\Models\Subrange $subrange * @return \Illuminate\Http\RedirectResponse */ public function destroy(Subrange $subrange): \Illuminate\Http\RedirectResponse { /** @var array */ $previousQueries = []; \parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries); if ($subrange->deleteOrFail()) { return redirect()->route('bo.subranges.index', $previousQueries) ->with('success', trans('crud.messages.has_been_deleted', [ 'model' => Str::ucFirst(trans('models.classes.subrange')) ])); } return redirect()->back() ->with('error', trans('crud.messages.cannot_be_deleted', [ 'model' => Str::ucFirst(trans('models.classes.subrange')) ])); } /** * Duplicate the specified resource. * * @param \App\Models\Subrange $subrange * @return \Illuminate\Contracts\View\View */ public function duplicate(Subrange $subrange): \Illuminate\Contracts\View\View { return $this->create($subrange->replicate()); } }