十月CMS-仅显示附加到当前用户组的那些记录 [英] October CMS - Display only those records that are attached to the groups of the current user

查看:101
本文介绍了十月CMS-仅显示附加到当前用户组的那些记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要显示模型中附加到某些组的那些记录. (属于多对多"关系). (Movies模型的插件列表的后端页面) 我想获取当前用户的组并创建一个查询.

I need to display only those records in the model that are attached to some groups. ("belongsToMany" Relations). (backend page of plugin list of Movies model) I want to get the groups of the current user and create a query.

我的意思是

我使用关系"字段将记录附加到组.

I use Relations field to to attach records to groups.

即:

  • 我有一张桌子"elisseii_myplugin_movies".
  • 在电影"模型中,我创建了一个名为组"的关系字段.
  • 并创建了表"elisseii_myplugin_movies_groups".
  • 在电影"模型中使用了$ belongsToMany.

  • I have table "elisseii_myplugin_movies".
  • In the "Movies" model, I created a relation field with the name "groups".
  • and created the table "elisseii_myplugin_movies_groups".
  • Used $belongsToMany in the "Movies" model.

public $belongsToMany =[ 

    'groups' =>[ 
        'Elisseiidev\MyPlugin\Models\Groups', 
        'table' => 'elisseii_myplugin_movies_groups', 
        'order' => 'name'
    ]

];

  • 插件中的组"模型使用标准表"backend_user_groups".

  • The "Groups" model in the plug-in uses the standard table "backend_user_groups".

    现在的记录包含有关附加到它们的组的信息. 您可以看到视频教程,其中对此进行了说明)

    Now records have information about the groups attached to them. You can see the video tutorial, which says about it)

    我需要用户不能编辑不属于其组的记录. 代码必须是动态的.

    I need that users can not edit records that do not belong to their group. It is necessary that the code be dynamic.

    由于我正在学习,我需要详细的答案)) 预先感谢您的宝贵时间.

    I need a detailed answer, since I'm learning)) Thank you in advance for your time.

    推荐答案

    您需要将此方法添加到使用列表行为表示显示列表的控制器中.

    you need to add this method to the controller which is using list behaviour means shows the lists.

    public function listExtendQuery($query) {
        $user = \BackendAuth::getUser();
        $codes = $user->groups->pluck('code')->toArray();
        $query->whereHas('groups', function ($q) use ($codes){
            $q->whereIn('groups.code', $codes);
        });
    }
    

    listExtendQuery方法将使用我们针对显示给用户的列表的自定义条件扩展查询.

    this listExtendQuery method will extend query with our custom condition for list which is showed for user.

    在这里获取用户,然后提取code,因为它将是数组,用户可以有多个组.

    here we fetch Be user then we extract code as it will be array, user can have multiple groups.

    现在您的Movies表具有groups关系,因此它可以具有多个组,因此现在我们可以检查用户是否可以使用电影组代码,然后可以向他显示列表项.

    now your Movies table has groups relation so it can have multiple groups so now we can check that if that movies group code is available in user then we can show him list item.

    感染不需要创建新模型(Elisseiidev\MyPlugin\Models\Groups),如果不需要其他功能,则可以直接使用内置模型.

    infect you don't need to create new model(Elisseiidev\MyPlugin\Models\Groups) you can directly use in-built Model if you don't need additional functionality.

    您可以简单地喜欢

    public $belongsToMany = [
        'groups' => [
             Backend\Models\UserGroup::class, 
            'table' => 'elisseii_myplugin_movies_groups',
            'order' => 'name'
        ]
    ];
    

    让我知道它是否有效.

    这篇关于十月CMS-仅显示附加到当前用户组的那些记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆