File manager - Edit - /var/www/html/spdc/api/app/Http/Services/NupdecService.php
Back
<?php namespace App\Http\Services; use App\Classes\BaseServiceResponse; use App\Http\Requests\NupdecRequest; use App\Http\Resources\NupdecResource; use App\Models\Nupdec; use App\Models\Volunteer; use Exception; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\DB; use Illuminate\Http\Request; class NupdecService extends BaseService { public function index(): BaseServiceResponse { if (request()->has('search_term')) { $nupdecs = $this->withSearch(Nupdec::class, request()->get('search_term'), ['neighborhood']); $nupdecs = $nupdecs->with(['activities', 'actuations', 'volunteers'])->paginate(15); return $this->response( NupdecResource::collection($nupdecs), JsonResponse::HTTP_OK ); } $nupdecs = Nupdec::with(['activities', 'actuations', 'volunteers'])->paginate(15); return $this->response( NupdecResource::collection($nupdecs), JsonResponse::HTTP_OK ); } public function show(string $id) { $nupdec = Nupdec::findOrFail(['id' => $id])->first(); return $this->response( [ 'nupdec' => new NupdecResource($nupdec->load('volunteers')), ], JsonResponse::HTTP_OK ); } public function store(NupdecRequest $request): BaseServiceResponse { try { DB::beginTransaction(); $data = $request->all(); $nupdec = Nupdec::create($data); if (isset($data['volunteers'])) { $nupdec->volunteers()->sync($data['volunteers']); } } catch (Exception $exception) { DB::rollBack(); return $this->response( ["error" => $exception->getMessage()], JsonResponse::HTTP_INTERNAL_SERVER_ERROR ); } DB::commit(); return $this->response( new NupdecResource($nupdec->load('volunteers')), JsonResponse::HTTP_CREATED ); } public function update(Request $request, string $id): BaseServiceResponse { $data = $request->all(); $nupdec = Nupdec::findOrFail($id)->first(); if (isset($data['volunteers'])) { foreach ($data['volunteers'] as $volunteer) { $nupdec->volunteers()->attach($volunteer); } } if (isset($data['remove_volunteers'])) { foreach ($data['remove_volunteers'] as $volunteer) { $nupdec->volunteers()->detach($volunteer); } } $nupdec->update($data); return $this->response( new NupdecResource($nupdec->load('volunteers')), JsonResponse::HTTP_CREATED ); } public function delete(string $id) { try{ DB::beginTransaction(); $nupdec = Nupdec::findOrFail($id)->first(); $nupdec->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