Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/Livewire

Redirect After Action in Livewire v4

Navigate users after completing actions.

1 February 20261 min readLivewireاقرأ بالعربية

On this page

  1. Basic Redirect Methods
  2. Redirect with Flash Message
  3. Conditional Redirects

Redirect users after form submissions or actions in Livewire v4.

Basic Redirect Methods

php
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

php
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

php
public function submit(): void
{
    $this->validate();
    
    $user = User::create($this->form);
    
    if ($user->isAdmin()) {
        $this->redirectRoute('admin.dashboard');
    } else {
        $this->redirectRoute('user.dashboard');
    }
}
All articles

Keep reading

  • Authorization in Livewire v4 Components1 February 20261 min read
  • Blaze Pre-Compiler: Massive Performance Gains in Livewire v41 February 20261 min read
  • Building a Bookmark Feature with Livewire v41 February 20261 min read