Laravel Capstone: A Tasks App
A tasks app joins the pieces of this course into one small app. It lists tasks, adds a task, and checks the title.
The task list
The index page shows every task title.
public function index(): View
{
return view('tasks.index', [
'tasks' => Task::orderBy('title')->get(),
]);
}The create form
The create page shows a form for a new task.
public function create(): View
{
return view('tasks.create');
}Saving a task
An empty title fails the check, and the form shows the error.
$validated = $request->validate([
'title' => 'required|max:255',
]);
Task::create($validated);Example
use App\Models\Task;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\View\View;
class TaskController extends Controller
{
public function index(): View
{
return view('tasks.index', [
'tasks' => Task::orderBy('title')->get(),
]);
}
public function create(): View
{
return view('tasks.create');
}
public function store(Request $request): RedirectResponse
{
$validated = $request->validate([
'title' => 'required|max:255',
]);
Task::create($validated);
return redirect('/l50/tasks');
}
}
Route::get('/l50/tasks', [TaskController::class, 'index']);
Route::get('/l50/tasks/create', [TaskController::class, 'create']);
Route::post('/l50/tasks', [TaskController::class, 'store']);Key points
- Capstone: one small app that joins the pieces of a course.
- Use a controller to hold the task pages.
- Use a Blade view to show the task list.
- Use validation to check the title before saving.
In this example, the controller holds three task pages. The index method loads every task in title order. The create method shows the form. The store method checks the title, saves the task, and sends the visitor back to the list. A tasks app joins the pieces of this course into one small app. Visit /l50/tasks in the browser to see the output.
Note
The list shows titles only, so it stays easy to read.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does the store method do first?