Nova 相关过滤 - 如何过滤 company_id 与位置 company_id 匹配的用户

C. *_*ill 7 php laravel laravel-nova

我有用户和位置之间的多对多关系。我想过滤我的“附加用户”选择列表以仅显示company_idcompany_id我当前所在位置匹配的用户。我创建了一个静态函数relatableUsers,它在查询中添加了一个“where”子句。

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

如何将当前位置拉company_id入此 where 查询中?如果我company_id在过滤器下方进行硬编码,那么我知道这是可能的。

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

记录信息不存储在$request或 中$query

编辑 - 添加关系

用户模型:

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

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

}
Run Code Online (Sandbox Code Playgroud)

定位模型:

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

    public function users()
{

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

}
Run Code Online (Sandbox Code Playgroud)

公司型号关系:

public function users()
{

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

}


public function location()
{

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

}
Run Code Online (Sandbox Code Playgroud)

Sau*_*nam 6

您可以Location从请求中检索目标,如下所示。

public static function relatableUsers(NovaRequest $request, $query)
{
    $location = $request->findResourceOrFail(); // Retrieve the location instance
    return $query->where('company_id', $location->company_id);
}
Run Code Online (Sandbox Code Playgroud)