DepartmentController.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Http\Controllers\API;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\API\Department\CreateOrUpdateRequest;
  5. use App\Http\Resources\API\DepartmentResource;
  6. use App\Models\Department;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Auth;
  9. class DepartmentController extends Controller
  10. {
  11. public function index(Request $request)
  12. {
  13. $department=Department::filter($request->all())->where("parent_id",0)->with(['children'])->simplePaginate();
  14. return DepartmentResource::collection($department);
  15. }
  16. public function store(CreateOrUpdateRequest $request)
  17. {
  18. $department=new Department();
  19. $department->mergeFillable([
  20. 'company_id'
  21. ]);
  22. $department->fill([
  23. ...$request->all(),
  24. 'company_id' => Auth::user()->company_id,
  25. ]);
  26. $department->save();
  27. return $this->created();
  28. }
  29. public function show(string $id)
  30. {
  31. $department=Department::query()->findOrFail($id);
  32. return new DepartmentResource($department);
  33. }
  34. public function update(CreateOrUpdateRequest $request,string $id)
  35. {
  36. $department=Department::findOrFail($id);
  37. $department->fill($request->all());
  38. $department->save();
  39. return $this->noContent();
  40. }
  41. public function destroy(string $id)
  42. {
  43. $department = Department::findOrFail($id);
  44. $department->delete();
  45. return $this->noContent();
  46. }
  47. }