Search code examples
phplaravellaravel-breeze

Laravel Breeze routes overlapping


I'm getting this error suddenly when trying to use anything to do with Breeze, like login and view account.

Declaration of App\Http\Controllers\ProfileController::update(App\Http\Requests\ProfileUpdateRequest $request): Illuminate\Http\RedirectResponse must be compatible with App\Http\Controllers\Controller::update(Illuminate\Http\Request $request, $id)

ProfileController.php

/**
     * Update the user's profile information.
     */
    public function update(ProfileUpdateRequest $request): RedirectResponse
    {
        $request->user()->fill($request->validated());

        if ($request->user()->isDirty('email')) {
            $request->user()->email_verified_at = null;
        }

        $request->user()->save();

        return Redirect::route('profile.edit')->with('status', 'profile-updated');
    }

On the 'ProfileUpdateRequest $request' it's giving me this error also:

Declaration must be compatible with Controller->update(request: \Illuminate\Http\Request, id)

I've tried Googling to find anyone with a similar issue but I'm unable to. I've compared my routes file with the official Breeze docs to no avail.


Solution

  • Routing is not the issue here, you have probably defined an update() method in the base Controller.

    Your ProfileController extends the base Controller. You have defined an update() method in both of these classes.

    This makes it so that the declaration of the update() method in the ProfileController must match the declaration of the update() method in the base Controller class, because it extends it.

    Looking at the error message in the post, we can compare the two declarations:

    public function update(ProfileUpdateRequest $request): RedirectResponse {} // ProfileController
    public function update(Illuminate\Http\Request $request, $id) {} // Controller
    

    The request class is not an issue here. Provided that the ProfileUpdateRequest extends the Illuminate\Http\Request class.

    Your ProfileController does not use an $id and defines a return type, thus the declaration is different from the class that it extends (App\Http\Controllers\Controller). This results in the error.

    Your base App\Http\Controllers\Controller should not define any methods. It is merely there so you can extend it in your Controllers and get the functionality from it. Removing the update() method from the base Controller class will fix your error. You should not use the base Controller itself to handle requests, but create a new controller that extends the base one.