Search code examples
laravelvoyager

Change voyager model


I want to add some mutators on Voyager's User class, but I won't change anything inside vendor folder, and Voyages uses a User model inside the package. Is it possible for me to, somehow, change this?


Solution

  • Simple enough. Modify the default Laravel User model to

    <?php
    
    namespace App\Models;
    
    use TCG\Voyager\Models\User as VoyagerUser;
    
    class User extends VoyagerUser {
        // add custom mutators and other code in here
    }
    

    Then you can update app/config/voyager.php as @aimme said and have it as

    'user' => [
        'add_default_role_on_register' => true,
        'default_role'                 => 'user',
        'admin_permission'             => 'browse_admin',
        'namespace'                    => App\User::class,
    ],
    

    This way you bring in the power of Voyager's User model to your own with no hackery involved.