Livewire Breaks Redirect conventions (Flash) in Laravel
I wanted to offer another constructive critique of the ecosystem, because sometimes I feel like, when I'm working, whether with Livewire or directly with the Laravel base, I'm just doing it to stay within the flow. Of course, you can also add Inertia, although Inertia seems a little more organized to me… until we get to the data sharing part, where everything gets crazy with the handleRequest. But hey, whatever.
In this case, I'm working with a Livewire project, and we're going to compare it a bit with a Laravel project, specifically with an operation as simple as updating a resource and displaying a Flash-like message. We can do this in any web framework, and it has the characteristic that the message only lasts for one request; therefore, if I reload the page, the message disappears. It's that simple.
Redirects in Laravel Base: Simple and Elegant
The Laravel base is a beautiful thing. In fact, you can see it here:
$this->redirect()->back();
There are many ways to do this, but I think the one I'm showing you is the most commonly used. From the request object, we access the session, use Flash, and set the message. It's that simple.
We can also do it using a redirect. Here, for example, we use redirect()->route(...). There are several ways, and Laravel has simplified the process a bit to make it more user-friendly.
Personally, I've always really liked this method: redirect()->back(), because generally, when we display a Flash message, we want to return to the previous page.
For example, if we're in the create or edit view, when we click "save," we go to another route that processes the request, and then we return to the same route to confirm that everything went well.
The same goes for the delete action. And it's very clean and reusable: instead of defining the route manually, we simply use back() and that's it.
Redirects and Flash Messages in Livewire: A Little Nightmare
But in Livewire, things become a bit of a nightmare. That's a bit of a criticism: we can't apply these fancy syntaxes. Here things get a bit more complicated.
For example, we don't directly access the request to use the session and then Flash, but rather directly access the session.
Here we set the message, but we can't configure it directly from the path, as I showed you in the previous example in the book. It can't be done that way.
Obviously, this is a reduced form. What we use in Livewire would be more manual, and there are surely other alternatives, but this is the one I know of:
session()->flash('status', __("Category successfully saved."));
return $this->redirect(url()->previous());
Because—again—for a single task, there are like 10 different ways! And these are just a few.
So, yeah... it's a bit confusing.
But the worst thing isn't that there are so many ways; that can even be a good thing. The problem is that they don't work well.
When things don't work...
For example, in Lware, if I try to redirect from somewhere else—not from where Laravel expects—the flash message stops working for some reason.
Here, in my case, I had no errors. Although it told me there was an error, it was simply that the tir variable didn't exist in the component scope.
I corrected that and used redirect directly. And still, the flash message no longer works.
So, nothing. I could give more examples, but this is basically what I wanted to say: it gets pretty tiresome having to reinvent the wheel, trying to figure out how the hell to make it work as expected. Literally, when I was doing this adaptation, it took me like half an hour just to get the blessed flash message to display.