12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace App\Http\Controllers\API;
- use App\Http\Controllers\Controller;
- use App\Http\Requests\API\Department\CreateOrUpdateRequest;
- use App\Http\Resources\API\DepartmentResource;
- use App\Models\Department;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- class DepartmentController extends Controller
- {
- public function index(Request $request)
- {
- $pageSize=$request->get('page_size') ?? 10;
- $department=Department::filter($request->all())->where("parent_id",0)->with(['children'])->paginate($pageSize);
- return DepartmentResource::collection($department);
- }
- public function store(CreateOrUpdateRequest $request)
- {
- $department=new Department();
- $department->mergeFillable([
- 'company_id'
- ]);
- $department->fill([
- ...$request->all(),
- 'company_id' => Auth::user()->company_id,
- ]);
- $department->save();
- return $this->created();
- }
- public function show(string $id)
- {
- $department=Department::query()->findOrFail($id);
- return new DepartmentResource($department);
- }
- public function update(CreateOrUpdateRequest $request,string $id)
- {
- $department=Department::findOrFail($id);
- $department->fill($request->all());
- $department->save();
- return $this->noContent();
- }
- public function destroy(string $id)
- {
- $department = Department::findOrFail($id);
- if(!empty($department->children()->first())){
- throw new \Exception("Please remove the sub-level departments.");
- }
- $department->delete();
- return $this->noContent();
- }
- }
|