In this episode, we're tackling how to let users post new comments. We kick things off by setting up the comments form in our template, making sure it only appears for authenticated users. We're using Laravel Breeze's component approach, so instead of a regular input, we create a reusable TextArea
component to keep our forms DRY and styled consistently.
Next, we add validation errors using Breeze's error component, and set up the form's submit button. But instead of handling everything in the main Livewire component, we keep things neat by creating a separate Livewire form class specifically for creating comments. This means our form logic, validation rules, and properties are all separated out, making the code clean and easier to maintain.
We wire up everything so that submitting the form fires the correct Livewire method, validates the data, and then actually creates the comment. We also talk about associating the new comment with the currently logged-in user, adding the necessary relationship to the Comment model to make this straightforward.
We do a quick test: try to submit a comment, see a mass assignment exception, and fix it by updating our model's guarded properties. Finally, we confirm everything works by checking the database and ensuring the new comment is stored with all the right data linked up.
By the end, you'll have a flexible, reusable comment form that’s properly validated, and all the logic nicely organized in dedicated classes.