PATH:
home
/
rwabteecom
/
project_11
/
app
/
Services
/
Staff
/
Editing: StaffServices.php
<?php namespace App\Services\Staff; use App\Interfaces\ServiceInterface; use App\Models\Admin; use Carbon\Carbon; use DB; use Exception; use Hash; use Illuminate\Support\Collection; class StaffServices implements ServiceInterface { public function index($active = false): Collection { return Admin::query()->get(); } public function findOrFail($id, $active = false, $withTrashed = false) { return Admin::query() ->when($active, function ($query) { return $query->where("status", 1); }) ->when($withTrashed, function ($query) { return $query->withTrashed(); }) ->find($id); } public function findWithoutFail($id, $active = false, $withTrashed = false) { return Admin::query() ->when($active, function ($query) { return $query->where("status", 1); }) ->when($withTrashed, function ($query) { return $query->withTrashed(); }) ->find($id); } public function store($request): void { DB::transaction(function () use ($request) { $admin = Admin::query()->create([ "name" => $request->name, "status" => 1, "username" => $request->username, "email" => $request->email, "email_verified_at" => Carbon::now(), "password" => Hash::make($request->password) ]); $admin->syncRoles($request->role_id); }); } public function update($id, $request): void { DB::transaction(function () use ($request, $id) { $admin = $this->findOrFail($id); if ($admin->id == 1) { throw new Exception(__("This staff cannot be updated")); } $admin->update([ "name" => $request->name, "username" => $request->username, "email" => $request->email, "email_verified_at" => Carbon::now(), "password" => $request->filled("password") ? Hash::make($request->password) : $admin->password, ]); $admin->syncRoles($request->role_id); }); } public function destroy($id): void { DB::transaction(function () use ($id) { $admin = $this->findOrFail($id); if ($admin->id == 1) { throw new Exception(__("This staff cannot be deleted")); } $admin->update([ "status" => 0, ]); $admin->delete(); }); } public function restore($id): void { DB::transaction(function () use ($id) { $admin = $this->findOrFail(id: $id, withTrashed: true); $admin->update([ "status" => 0, ]); $admin->restore(); }); } }
SAVE
CANCEL