Tailor Entry with a relationship to the Model Rainlab/User

Hi
I just wold like to create a blueprint called Notes and attach a Rainlab/User to it.
Is this possible easily or is it better to take the path of the plugin dev here?

1 Like

I was able to achieve this by doing a “relation”. I’m not sure if there’s a better way (like building a function for it in a plugin) but it works well.

I set my tailor field up like this:

user_owner:
    label: Owner
    type: recordfinder
    list: ~/plugins/rainlab/user/models/user/columns.yaml
    recordsPerPage: 10
    title: Find User
    tab: Manage
    descriptionFrom: email
    useRelation: false
    modelClass: RainLab\User\Models\User
    disabled: false
    span: right
    commentAbove: The owner of this rental.
    conditions: rental_owner = 1

Then whenever I need the user, I can use the field to look up the id.

use Rainlab\User\Models\User;
use Tailor\Models\EntryRecord;

function onStart() {
    $model = EntryRecord::inSection('Your\Handle');
    $user = User::find($model->$user_owner);
    $this['owner'] = $user;
}

or if I need it in twig, I’ll access it like this:

{% set user = users.find(model.user_owner) %}

Edit: forgot some stuff

Fantastic! Thanks @artistro08
Will try that.

in the twig version, where does the variables users come from?

1 Like

From the model but you’ll have to return it in your view


use Rainlab\User\Models\User;

#...

$this['users'] = User::all();