Search code examples
phplaravellaravel-livewire

Laravel solve correct return class definitation


In one class which i used in Laravel i have this method:

public function submit(): \Illuminate\Http\RedirectResponse
{
    $this->validate($this->rules);
    if (Auth::attempt(['username' => $this->username, 'password' => $this->password])) {
        $this->emmitMessage('', 'Your Welcome', 'success');
        return redirect()->route('administrator','en');
    } else {
        $this->emmitMessage('', 'Username or password is incorrect', 'error');
    }
}

private function emmitMessage($title = '', $text = '', $type = 'info')
{
    $this->emit('registerNotification', [
        // ...
    ]);
}

in this method which i have return redirect()->route('administrator','en') that's instance of \Illuminate\Http\RedirectResponse and after successful login i get this error:

App\Http\Livewire\Auth\LoginComponent::submit(): 
    Return value must be of type Illuminate\Http\RedirectResponse,
    Livewire\Redirector returned 

i think this is correct but i can't fix this error


Solution

  • I haven't used Laravel Liwewire yet, but I believe it somehow overrides default Laravel redirection system, so:

    return redirect()->route('administrator','en');
    

    is returning custom Liwewire redirect response.

    That's why probably instead of:

    public function submit(): \Illuminate\Http\RedirectResponse
    

    you should try with just:

    public function submit()
    

    or

    public function submit(): \Livewire\Redirector
    

    to make it work