Redirect users after form submissions or actions in Livewire v4.
Basic Redirect Methods
class CreatePost extends Component
{
public string $title = '';
public string $content = '';
public function save(): void
{
$post = Post::create([
'title' => $this->title,
'content' => $this->content,
]);
// Simple redirect to URL
$this->redirect('/posts');
// Redirect with SPA-like navigation (no full page reload)
$this->redirect('/posts', navigate: true);
// Redirect to named route
$this->redirectRoute('posts.index');
// Redirect to route with parameters
$this->redirectRoute('posts.show', ['post' => $post->id]);
// Redirect with intended URL (after login)
$this->redirectIntended(default: '/dashboard');
}
}Redirect with Flash Message
public function save(): void
{
Post::create($this->only(['title', 'content']));
session()->flash('success', 'Post created successfully!');
$this->redirect('/posts', navigate: true);
}
// Or use redirect with session helper
public function delete(Post $post): void
{
$post->delete();
return $this->redirectRoute('posts.index')
->with('message', 'Post deleted!');
}Conditional Redirects
public function submit(): void
{
$this->validate();
$user = User::create($this->form);
if ($user->isAdmin()) {
$this->redirectRoute('admin.dashboard');
} else {
$this->redirectRoute('user.dashboard');
}
}