0

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

A category has more than one user, and a user belongs to a category

I add category_id in the user table

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

I use the RainLab user plugin

Help me please,

thank you in advance

2 Answers2

3

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

    });
}

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.

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
1

It is very easy with OctoberCMS relationships. I think you have defined the relationship in your User Model, if not defined it first like below,

    public $belongsTo = [
        'catgeory' => 'YourPluginName\YourAuthorName\Models\Category'
    ];

In place of YourPluginName & YourAuthorName, please define your author and plugin name.

After defining the relationship, you can retrieve user's category by $user->category

e.g. Suppose you have the first User then you can retrieve category by,

$user = User::first();
$category = $user->category;

//and now you can get the category name and all fields by $category,
//$category->title;
//$catgory->name; 

And if you are using twig in the page, and you are showing users with {{ user.name}}(field may be different) then you have to just use {{ user.category.name}} (field may be different) and you can show category too.

I hope you will get categories with users. If you find any difficulty please comment.

Chirag Patel
  • 1,545
  • 2
  • 9
  • 17
  • 1
    hi chirag you added code With `Category` model and added again category relation in same model, I guess it should be `User` model , also `rainlab.user` is separate plugin so you dont wish to add direct modification to it ... its better to extend `rainlab.user` plugin so in future plugin update will not cause any problem. – Hardik Satasiya Apr 23 '18 at 06:36
  • 2
    Thanks for the suggestion, it was a typing mistake. And I think questioner has already defined the relationship and he has no idea about retrieving user's category. – Chirag Patel Apr 23 '18 at 07:08
  • 1
    @LutteP.Okamango Great feeling happy for you :) please upvote & accept answer as a appreciation – Chirag Patel Apr 23 '18 at 18:36
  • 1
    yes Hardik is right because if Rainlab.User plugin get update then the code written on user model for relation will be lost so this relationship must be added on his custom plugin's boot method. – Zakir hussain May 07 '18 at 09:45