Narrow Relationship Results In Octobercms Form By Current Backend User

- 1 answer

I have these three models:

  1. Teacher: belongs_to: 'user' => 'Backend\Models\User',
    has_many: 'classes' => 'Acme\School\Models\Class'
  2. Classes: belongs_to: 'teacher' => 'Acme\People\Models\Teacher',
  3. Schedule: belongs_to: 'class' => 'Acme\School\Models\Class'

I'm trying to build a form for model Schedule where a teacher can select only from his classes in order to complete his schedule. For the moment I have this in schedule/fields.yaml:

        label: Class
        nameFrom: full_name
        span: auto
        required: 1
        type: relation

I tried adding scope to the field definition but in Model\Classes I don't seem to have access to Request, User or Auth.

  • please note that Class is not the real model name, nor classes the relationship's name.
  • if the title of my question is not good enough please recommend me another one.
  • if the solution is an easy and obvious one I think I'll shoot myself.


I have something similar and solved it with an scope, in the scope you have access to the Backend User with BackendAuth::getUser()

I have this scope:

public function scopeHasAccess($query)
    if (BackendAuth::getUser()->is_superuser) {

    $query->whereHas('clients', function ($query) {
        $query->where('user_id', BackendAuth::getUser()->id);

But I'm doing the scope only in the backend relation implementing the RelationController Behaviour