It seems like you're trying to implement a redirect after login based on the URL the user came from. The issue you're facing with the hidden input and Livewire's wire:submit is likely due to the way Livewire handles state and re-renders components.
Here's a solution that should work for you:
- Store the intended URL in the session before showing the login form. You can do this in your middleware or controller where you handle the redirection to the login page.
// In your controller or middleware
session(['url.intended' => request()->query('url', url()->previous())]);
-
In your login form, you don't need to include the redirect URL as a hidden input. Livewire will maintain the state for you.
-
In your Livewire component's
createmethod (or the method that handles the login), you can retrieve the intended URL from the session and use it to redirect the user after a successful login.
public function create()
{
// Perform the login logic...
// After successful login, redirect to the intended URL
return redirect()->to(session()->pull('url.intended', '/default-redirect-path'));
}
- If you're using the built-in Laravel authentication, you can customize the
authenticatedmethod in yourLoginControllerto handle the redirection:
protected function authenticated(Request $request, $user)
{
return redirect()->intended('/default-redirect-path');
}
Laravel's redirect()->intended() method automatically uses the url.intended session key to redirect the user to the intended URL after login, or to the default path if the session key is not set.
This approach should solve the problem of the URL being lost on re-render, as the intended URL is stored in the session and not in the component's state. It also leverages Laravel's built-in functionality to handle post-login redirections.