This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
memberdb/app/Scoping.php

40 lines
981 B
PHP
Raw Normal View History

<?php namespace App/Scoping;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\ScopeInterface;
class TenantScope implements ScopeInterface {
private $model;
protected $tenant_col = 'tenant_id';
public function apply(Builder $builder, Model $model)
{
dd(__METHOD__);
$builder->whereRaw($model->getTenantWhereClause($this->tenant_col, 1));
}
public function remove(Builder $builder, Model $model)
{
dd(__METHOD__);
$query = $builder->getQuery();
foreach( (array) $query->wheres as $key => $where) {
if($this->isTenantConstraint($model, $where, $this->tenant_col, 1)) {
unset($query->wheres[$key]);
$query->wheres = array_values($query->wheres);
break;
}
}
}
public function isTenantConstraint($model, array $where, $tenantColumn, $tenantId)
{
dd(__METHOD__);
return $where['type'] == 'raw' && $where['sql'] == $model->getTenantWhereClause($tenantColumn, $tenantId);
}
}