dongshan1396 2018-11-06 21:43
浏览 94
已采纳

Nova Relatable Filtering - 如何过滤company_id与位置company_id匹配的用户

I have a many to many relationship between users and locations. I want to filter my "attach user" select list to only show users whose company_id matches the company_id of the location I am currently on. I have created a static function called relatableUsers which adds a "where" clause to the query.

public static function relatableUsers(NovaRequest $request, $query)
{
    return $query->where('company_id', ???);
}

How do I pull the current location's company_id into this where query? If I hard code a company_id like below the filter works, so I know this is possible.

public static function relatableUsers(NovaRequest $request, $query)
{
    return $query->where('company_id', 'FA624E60-DD37-11E8-A540-C3C0A709EE15');
}  

The record information is not stored in the $request or the $query.

EDIT - adding relationships

User model:

public function company()
{
    return $this->belongsTo(Company::class);
}

    public function locations()
{
    return $this->belongstoMany(Location::class);

}

Location model:

public function company()
{
    return $this->belongsTo(Company::class);
}

    public function users()
{

    return $this->belongstoMany(User::class);

}

Company model relationships:

public function users()
{

    return $this->hasMany(User::class);

}


public function location()
{

    return $this->hasMany(Location::class);

}
  • 写回答

2条回答 默认 最新

  • dsyrdwdbo47282676 2018-11-09 06:20
    关注

    You can retrieve the target Location from the request like below.

    public static function relatableUsers(NovaRequest $request, $query)
    {
        $location = $request->findResourceOrFail(); // Retrieve the location instance
        return $query->where('company_id', $location->company_id);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)