testapi/app/Http/Controllers/DepartmentController.php

109 lines
2.7 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Inertia\Inertia;
use App\Models\Department;
class DepartmentController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request) {
$departments = Department::orderBy('name')
->paginate(10)
->through(fn($dept) => [
'id' => $dept->id,
'name' => $dept->name,
]);
return Inertia::render('Department/Index', [
'departments' => $departments,
'filters' => $request->only(['page']),
]);
}
/**
* Show the form for creating a new resource.
*/
public function create() {
return Inertia::render('Department/Create');
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request) {
$data = $request->validate([
'name' => 'required|string|max:255',
]);
Department::create($data);
return redirect()
->route('smartdok.departments.index')
->with('success', 'Department created successfully.');
}
/**
* Display the specified resource.
*/
public function show(string $id) {
$department = Department::findOrFail($id);
return Inertia::render('Department/Show', [
'department' => [
'id' => $department->id,
'name' => $department->name,
],
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id) {
$department = Department::findOrFail($id);
return Inertia::render('Department/Edit', [
'department' => [
'id' => $department->id,
'name' => $department->name,
],
]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id) {
$department = Department::findOrFail($id);
$data = $request->validate([
'name' => 'required|string|max:255',
]);
$department->update($data);
return redirect()
->route('smartdok.departments.index')
->with('success', 'Department updated successfully.');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id) {
$department = Department::findOrFail($id);
$department->delete();
return redirect()
->route('smartdok.departments.index')
->with('success', 'Department deleted successfully.');
}
}