In this episode, we're diving into how to handle form submissions with Inertia.js in a Laravel app. We start by building a basic form with a textarea for the body of the post (nothing fancy, just enough to get us going), and styling it a bit using Tailwind. Then, we add a submit button to allow users to post their content.
Next up, instead of just letting the browser handle the form submission in the old-fashioned way, we set things up so Inertia can capture the submit event and send the form data to the backend using an AJAX request. To do this, we use the useForm
composable from Inertia (imported in our script), which makes managing form state and submissions super easy.
We wire everything up: the textarea is connected to our form data with v-model, and the form's submit event triggers a custom post
method. On the backend, we add a simple controller and route that catches the POST request. We first just dump the data to make sure it's coming through, then switch to actually creating a new post in the database using Laravel's request handling.
By the end of the episode, we have form data being sent to the backend, posts being created in the database, and everything is hooked up nicely with Inertia. It's not quite production-ready (we don’t yet handle validation or updates to the post list), but it’s a very solid start. Next time, we'll tackle form validation and making things look and work even better!