laravel 更改航海者模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41753108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:10:16  来源:igfitidea点击:

Change voyager model

laravelvoyager

提问by Guilherme Pressutto

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?

我想在 Voyager 的 User 类上添加一些修改器,但我不会更改 vendor 文件夹中的任何内容,并且 Voyages 在包中使用了 User 模型。我有可能以某种方式改变这一点吗?

回答by Theson

Simple enough. Modify the default Laravel User model to

足够简单。修改默认的 Laravel User 模型为

<?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.phpas @aimme said and have it as

然后你可以app/config/voyager.php像@aimme 所说的那样更新并把它作为

'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.

通过这种方式,您可以将 Voyager 用户模型的强大功能引入您自己的模型,而无需涉及黑客。

回答by aimme

For most cases extending the vendor packages works fine.

对于大多数情况,扩展供应商包工作正常。

Update voyager configuration

更新航海者配置

See config/voyager.php

config/voyager.php

change user array like this. i have changed only namespacehere,from voyager user tp App\User.

像这样更改用户数组。我只namespace在这里更改,来自航海者用户 tp App\User。

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

but to bring additional changes you could try following way.

但要带来额外的变化,您可以尝试以下方式。

Loading whole voyager package as a local package.

将整个 voyager 包作为本地包加载。

Warning: After doing this the package would no longer be a vendor package.

警告:执行此操作后,包将不再是供应商包。

To do that

要做到这一点

1 - create a packages folder on the root of the project.

1 - 在项目的根目录上创建一个包文件夹。

2 - clone the tcg/voyager repo into the packages folder or cut paste vendor tcg folder into packages folder that you created. so you will be having your directories like this yourproject/packages/tcg/voyager. if you have required tcg/voyager in composer.json remove it from there.

2 - 将 tcg/voyager 存储库克隆到包文件夹或将供应商 tcg 文件夹粘贴到您创建的包文件夹中。所以你会有这样的目录yourproject/packages/tcg/voyager。如果您在 composer.json 中需要 tcg/voyager,请将其从那里删除。

3 - update composer.json file on the root of your project. add TCG\\Voyagerautoload. see below sample, added the line inside psr-4.

3 - 更新项目根目录下的 composer.json 文件。添加TCG\\Voyager自动加载。请参见下面的示例,在 psr-4 中添加了该行。

"autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\": "app/",
            "TCG\Voyager\": "packages/tcg/voyager/src/"
        }
    },

4 - run composer update

4 - 运行作曲家更新

sometimes you might have to do composer dump-autoloadafter bringing changes to the packages.

有时您可能需要composer dump-autoload在对包进行更改后执行此操作。