用户与类别OctoberCMS之间的关系 [英] relationship between user and category OctoberCMS

本文介绍了用户与类别OctoberCMS之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在用户和类别之间建立关系?

how to establish a relationship between the user and the category?

一个类别有多个用户,并且一个用户属于该类别

我在用户表中添加了category_id

I add category_id in the user table

在前端,我显示带有插件 builder 的用户列表. 但我也想显示用户的类别

on the frontend I display the list of users with the plugin builder but I also want to display the category of the user

我使用 RainLab 用户插件

请帮助我

先谢谢您

推荐答案

似乎您需要扩展用户模型并添加category关系运行时

it seems you need to extend user model and add category relation runtime

您需要将此代码添加到插件的启动方法中

You need to add this code to your plugin's boot method

public function boot()
{
    //Extending User Plugin
    \RainLab\User\Models\User::extend(function($model) {

        $model->hasOne['category'] = 'HardikSatasiya\Plugin\Models\Category';

    });
}

此代码将为用户模型添加动态关系,因此现在您可以像$userModel->category->name

this code will add dynamic relation to user model so now you can use this relation directly like $userModel->category->name

所以现在在列表中,您可以使用以下代码

so now in list you can use below code

{% for user in records %}
    Email : {{ user.email }}
    Category : {{ user.category.name }}
{% endfor %}

如有疑问,请发表评论.

if any doubt please comment.

这篇关于用户与类别OctoberCMS之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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