我在具有多个租户ID的单个数据库中将Lumen API与pgsql一起使用。我在model.php中使用范围函数,并在所有查询中使用此函数checkTenantid()
。它工作正常,但我不想在每个查询中都使用此功能。谁能建议我如何使用单一功能。
/** Check Tenant ID is exist or not **/
public function scopeCheckTenantid( $query, int $tenantid )
{
return $query->where('tenantid', '=', $tenantid);
}
PatientDataController
/* Get single entry of Heard About, Patient Status and Active Status */
public function get($patientId)
{
$record = $this->db
->checkTenantid($this->user->Tenantid)
->checkPatient($patientId)
->get();
return $record;
}
谢谢
如果对问题有任何疑问,请评论我。
我假设get调用用于存储库/服务,您可以利用QueryBuilders when()
方法。仅在条件正确的情况下,此方法才会执行查询,为此,您需要传递条件。
public function get($patientId, bool $withTenant) {
$record = $this->db
->when($withTenant, function($q) {
$q->checkTenantid($this->user->Tenantid)
})
->checkPatient($patientId)
->get();
return $record;
}