fixed common api endpoints

This commit is contained in:
Denis Duliçi
2020-12-26 16:13:34 +03:00
parent 8dbe178a70
commit 2e09989cf5
13 changed files with 267 additions and 113 deletions

View File

@ -2,12 +2,15 @@
namespace App\Scopes;
use App\Traits\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class Company implements Scope
{
use Scopes;
/**
* Apply the scope to a given Eloquent query builder.
*
@ -34,43 +37,11 @@ class Company implements Scope
}
// Skip if already exists
if ($this->exists($builder, 'company_id')) {
if ($this->scopeExists($builder, 'company_id')) {
return;
}
// Apply company scope
$builder->where($table . '.company_id', '=', session('company_id'));
}
/**
* Check if scope exists.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param $column
* @return boolean
*/
protected function exists($builder, $column)
{
$query = $builder->getQuery();
foreach ((array) $query->wheres as $key => $where) {
if (empty($where) || empty($where['column'])) {
continue;
}
if (strstr($where['column'], '.')) {
$whr = explode('.', $where['column']);
$where['column'] = $whr[1];
}
if ($where['column'] != $column) {
continue;
}
return true;
}
return false;
}
}