In this episode, we're diving into using the Laravel Actions package to tidy up some of our logic and make it super reusable. The focus is on handling the process of submitting a form, doing validation, and (eventually) sending an email magic link for logging in.
We start by chatting about why we want to use actions—mainly, to keep the process of sending a magic link in one testable place and to make it reusable for things like both logging in and registering users. We'll see how to install the Laravel Actions package, make a new action called SendMagicLink
, and let Laravel handle pretty much all of the boilerplate for us.
After that, we wire up this new action as a controller for the login form, tweaking the routes and form as needed. You'll see how to validate requests inside actions by defining rules (like checking if the email exists in the database) and how to nicely handle errors in the view.
We also touch on why structuring things this way makes your code easier to test and reuse later—for example, using the same action when registering a user. By the end, you'll have a neat, DRY approach to dealing with actions that will keep your codebase clean and maintainable.