authorizeResource(CompanySector::class); } /** * Display a listing of the resource. * * @param \Illuminate\Http\Request $request * @return \Inertia\Response */ public function index(Request $request): \Inertia\Response { /** @var \Illuminate\Database\Eloquent\Builder<\App\Models\CompanySector> $query */ $query = CompanySector::query()->with('companies'); if ($request->routeIs("back.$this->modelName.trash")) { $query = $query->onlyTrashed(); } /** @var string $search Search field */ $search = $request->search; if ($search) { $this->searchQuery( $query, $search, null, 'name', ); } $searchFields = \implode(', ', [ trans('validation.attributes.name'), ]); /** Sort columns with a query */ $this->sortQuery($query); /** Custom pagination */ $companySectorModels = $this->paginate($query); return Inertia::render('CompanySectors/Index', [ 'companySectorModels' => CommonResource::collection($companySectorModels), 'searchFields' => $searchFields, 'modelName' => $this->modelName, ]); } /** * Show the form for creating a new resource. * * @param \App\Models\CompanySector $company_sector * @return \Inertia\Response */ public function create(CompanySector $company_sector): \Inertia\Response { $companies = $company_sector->companies()->paginate(ItemsPerPaginationEnum::five->value()); return Inertia::render('CompanySectors/Create', [ 'data' => CommonResource::make($company_sector), 'companies' => CommonResource::collection($companies), 'modelName' => $this->modelName, ]); } /** * Show a specific resource. * * @param \App\Models\CompanySector $company_sector * @return \Inertia\Response */ public function show(CompanySector $company_sector): \Inertia\Response { $companies = $company_sector->companies()->paginate(ItemsPerPaginationEnum::five->value()); return Inertia::render('CompanySectors/Show', [ 'data' => CommonResource::make($company_sector), 'companies' => CommonResource::collection($companies), 'modelName' => $this->modelName, ]); } /** * Store a newly created resource. * * @param \App\Http\Requests\Back\CompanySectors\StoreCompanySectorRequest $request * @return \Illuminate\Http\RedirectResponse */ public function store(StoreCompanySectorRequest $request): \Illuminate\Http\RedirectResponse { return DB::transaction(function () use ($request) { $pageConfigModel = new CompanySector(); $pageConfigModel->fill($request->validated()); if ($pageConfigModel->saveOrFail()) { return redirect()->route("back.$this->modelName.edit", $pageConfigModel) ->with('success', __('crud.messages.has_been_created', [ 'model' => Str::of(trans('models.classes.company'))->plural()->ucfirst() ])); } return redirect()->back() ->with('error', __('crud.messages.cannot_be_created', [ 'model' => Str::of(trans('models.classes.company'))->plural()->ucfirst() ])); }); } /** * Show the form for editing the specified resource. * * @param \App\Models\CompanySector $company_sector * @return \Inertia\Response */ public function edit(CompanySector $company_sector): \Inertia\Response { return Inertia::render('CompanySectors/Edit', [ 'data' => CommonResource::make($company_sector), 'modelName' => $this->modelName, ]); } /** * Update the specified resource. * * @param \App\Http\Requests\Back\CompanySectors\UpdateCompanySectorRequest $request * @param \App\Models\CompanySector $company_sector * @return \Illuminate\Http\RedirectResponse */ public function update( UpdateCompanySectorRequest $request, CompanySector $company_sector, ): \Illuminate\Http\RedirectResponse { return DB::transaction(function () use ($request, $company_sector) { $company_sector->fill($request->validated()); if ($company_sector->saveOrFail()) { return redirect()->route("back.$this->modelName.edit", $company_sector) ->with('success', __('crud.messages.has_been_updated', [ 'model' => Str::of(trans('models.classes.company'))->ucfirst() ])); } return redirect()->route("back.$this->modelName.edit", $company_sector) ->with('error', __('crud.messages.cannot_be_updated', [ 'model' => Str::of(trans('models.classes.company'))->ucfirst() ])); }); } /** * Duplicate the specified resource. * * @param \App\Models\CompanySector $company_sector * @return \Inertia\Response */ public function duplicate(CompanySector $company_sector): \Inertia\Response { return $this->create($company_sector->replicate()); } /** * Restore the specified resource. * * @param integer $company_sector * @return \Illuminate\Http\RedirectResponse */ public function restore(int $company_sector): \Illuminate\Http\RedirectResponse { $company_sector = CompanySector::withTrashed()->findOrFail($company_sector); /** @var array */ $previousQueries = []; \parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries); if ($company_sector->restore()) { return redirect()->route("back.$this->modelName.trash", $previousQueries) ->with('success', __('crud.messages.has_been_restored', [ 'model' => Str::of(trans('models.classes.company'))->plural()->ucfirst(), ])); } return redirect()->back() ->with('error', __('crud.messages.cannot_be_restored', [ 'model' => Str::of(trans('models.classes.company'))->plural()->ucfirst(), ])); } /** * Destroy the specified resource. * * @param \App\Models\CompanySector $company_sector * @return \Illuminate\Http\RedirectResponse */ public function destroy(CompanySector $company_sector): \Illuminate\Http\RedirectResponse { /** @var array */ $previousQueries = []; \parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries); if ($company_sector->delete()) { return redirect()->route("back.$this->modelName.index", $previousQueries) ->with('success', __('crud.messages.has_been_deleted', [ 'model' => Str::of(trans('models.classes.company'))->plural()->ucfirst(), ])); } return redirect()->back() ->with('error', __('crud.messages.cannot_be_deleted', [ 'model' => Str::of(trans('models.classes.company'))->plural()->ucfirst(), ])); } /** * Destroy the specified resource. * * @param \App\Models\CompanySector|integer $company_sector * @return \Illuminate\Http\RedirectResponse */ public function forceDestroy(CompanySector|int $company_sector): \Illuminate\Http\RedirectResponse { if (is_int($company_sector)) { $company_sector = CompanySector::withTrashed()->findOrFail($company_sector); } /** @var array */ $previousQueries = []; \parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries); if ($company_sector->forceDelete()) { return redirect()->route("back.$this->modelName.trash", $previousQueries) ->with('success', __('crud.messages.has_been_deleted', [ 'model' => Str::of(trans('models.classes.company'))->plural()->ucfirst(), ])); } return redirect()->back() ->with('error', __('crud.messages.cannot_be_deleted', [ 'model' => Str::of(trans('models.classes.company'))->plural()->ucfirst(), ])); } }