This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

stefan19666
stefan19666

Hi all,

I try to setup a simple HR applications with companys, offices and employees. So the companys have employees and offices and the employees work in specific offices. I built all the relationships and forms with builder plugin and everything works fine. I do edit the offices and employees within tabs of the company. But when i want to select the specific offices for the employees they work in, I get them all (of all companys) and not only of the company they work in. So I need a filter / dynamic scope which accepts the "office_id" the current employee object provides, but i just do not get it, how to use scopes dynamically.

Thanks for your help :)

Stefan

stefan19666
stefan19666

Okay, got it to work by myself. Not satisfied with the solution but it has to be ok for now. Relation should be much faster in working with the dialogs, but I did not get it to work.

I switched to recordfinder instead of relation and removed the "scope" from the "belongsTo" declaration in the employee model.

I added the scope method in die fields.yaml:

scope: filterCompany // so that only the offices from the current company will be displayed

and in the Office Model I did add:

public function scopeFilterCompany($query, $term){
      return $query->where('company_id',$term->company->id) ;
}

It's not in the documentation that this method will also get the search term as a param: https://octobercms.com/docs/backend/forms#widget-recordfinder , but hey :)

And tbh. I do not know if this is the correct solution but it seems to work with the tested data objects I already have in the database.

rrk
rrk

@stefan19666 I'm having similar problem. Where did you call filterCompany method? I'm getting as method not found.

maxDubovsky
maxDubovsky

Works like sharm ) @rrk apply filter to the config_relation manage: showSearch: false showSorting: true defaultSort: name recordsPerPage: 40 list: ~/plugins/xxxx/xxxx/models/additionalservice/relation_columns.yaml scope: FilterCompany

Last updated

jobtest35451
jobtest35451

Hi,

public function scopeFilterCompany($query, $term){ return $query->where('company_id',$term->company->id) ; }

I found that the $term only work on update record. It always null when creating the record. Any ideas on it?

mitch
mitch

When creating the record you can find your fields in post() array

1-6 of 6

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.