Laravel/app/Http/Controllers/TodoController.php
2024-12-02 11:04:54 +01:00

33 lines
715 B
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Todo;
class TodoController extends Controller
{
public function index() {
$todos = Todo::all();
return view('todo', ['todos' => $todos]);
}
public function store(Request $request) {
$validated = $request->validate([
'task' => 'required|string|max:255'
]);
$todo = Todo::create([
'task' => $validated['task']
]);
return response()->json($todo);
}
public function destroy($id) {
$todo = Todo::findOrFail($id);
$todo->delete();
return response()->json(['message' => 'Task deleted successfully']);
}
}