Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kaju74's avatar

How to call the parent of an overridden trait method?

Hi,

I've overwritten the method "getLogout()" from the AuthenticatesUsers-Trait inside the AuthController-class. My problem is, that I'm not sure how to call the parent method inside the trait after my custom stuff?

Regards, Marc

0 likes
4 replies
bestmomo's avatar

Hello,

A trait is not a class, just a “copy and paste” code during run time, so dont try call it, just copy all code from trait to your method.

kaju74's avatar

Hi - yes, this is what I've done - thought, there will be a better solution due of possible sourcecode changes in the future...

Thank you, Marc

bobbybouwmann's avatar

That wouldn't affect your code, since you override the function from the trait, so the trait can be updated to whatever, your code will still work when you override the traits function

pawaniitd's avatar

If you copy and paste the trait's method you will not be able to make use of any future updates to Laravel's auth code. Here is my solution:

In this example i want to override 'logout' method.

class LoginController extends Controller
{
    use AuthenticatesUsers {
        logout as protected parent_logout;
        }


    // This is my method to add custom code before logout
    public function logout(Request $request)
    {
        // Custom code goes here


        // Finally call the trait's logout method
        return $this->parent_logout($request);
    }
}

This way you can add your customization and also get the benefits of updates to the laravel package.

6 likes

Please or to participate in this conversation.