40 lines
981 B
PHP
40 lines
981 B
PHP
<?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);
|
|
}
|
|
|
|
}
|