File manager - Edit - /var/www/html/spdc/api/app/Http/Services/VolunteerService.php
Back
<?php namespace App\Http\Services; use App\Classes\BaseServiceResponse; use App\Http\Requests\VolunteerRequest; use App\Http\Resources\VolunteerResource; use App\Models\Volunteer; use Exception; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class VolunteerService extends BaseService { public function index(): BaseServiceResponse { if (request()->has('search_term')) { $volunteers = $this->withSearch(Volunteer::class, request()->get('search_term'), ['name']); $volunteers = $volunteers->with('nupdecs')->paginate(15); return $this->response( VolunteerResource::collection($volunteers), JsonResponse::HTTP_OK ); } $volunteers = Volunteer::with('nupdecs')->paginate(15); return $this->response( VolunteerResource::collection($volunteers), JsonResponse::HTTP_OK ); } public function show(Volunteer $volunteer) { return $this->response( [ 'volunteer' => new VolunteerResource($volunteer->load('nupdecs')), ], JsonResponse::HTTP_OK ); } public function store(VolunteerRequest $request): BaseServiceResponse { try { DB::beginTransaction(); $data = $request->all(); $volunteer = Volunteer::create($data); if (isset($data["nupdecs"])) { $volunteer->nupdecs()->sync($data["nupdecs"]); } } catch (Exception $exception) { DB::rollBack(); return $this->response( ["error" => $exception->getMessage()], JsonResponse::HTTP_INTERNAL_SERVER_ERROR ); } DB::commit(); return $this->response( new VolunteerResource($volunteer->load('nupdecs')), JsonResponse::HTTP_CREATED ); } public function update(Request $request, string $id): BaseServiceResponse { try{ DB::beginTransaction(); $data = $request->all(); $volunteer = Volunteer::findOrFail($id)->first(); $volunteer->update($data); if (isset($data["nupdecs"])) { foreach ($data["nupdecs"] as $nupdec) { $volunteer->nupdecs()->attach($nupdec); } } if (isset($data["remove_nupdecs"])) { foreach ($data["remove_nupdecs"] as $nupdec) { $volunteer->nupdecs()->detach($nupdec); } } } catch(Exception $e){ DB::rollBack(); return $this->response( ["error" => $e->getMessage()], JsonResponse::HTTP_INTERNAL_SERVER_ERROR ); } DB::commit(); return $this->response( new VolunteerResource($volunteer->load('nupdecs')), JsonResponse::HTTP_CREATED ); } public function delete(string $id) { try{ DB::beginTransaction(); $volunteer = Volunteer::findOrFail($id)->first(); $volunteer->delete(); }catch(Exception $e){ DB::rollBack(); return $this->response( ["error" => $e->getMessage()], JsonResponse::HTTP_INTERNAL_SERVER_ERROR ); } DB::commit(); return $this->response( [], JsonResponse::HTTP_OK ); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings