add: full multi-tenancy control
This commit is contained in:
146
packages/Webkul/Core/src/Eloquent/Repository.php
Executable file
146
packages/Webkul/Core/src/Eloquent/Repository.php
Executable file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace Webkul\Core\Eloquent;
|
||||
|
||||
use Prettus\Repository\Contracts\CacheableInterface;
|
||||
use Prettus\Repository\Eloquent\BaseRepository;
|
||||
use Prettus\Repository\Traits\CacheableRepository;
|
||||
|
||||
abstract class Repository extends BaseRepository implements CacheableInterface
|
||||
{
|
||||
use CacheableRepository;
|
||||
|
||||
/**
|
||||
* Find data by field and value
|
||||
*
|
||||
* @param string $field
|
||||
* @param string $value
|
||||
* @param array $columns
|
||||
* @return mixed
|
||||
*/
|
||||
public function findOneByField($field, $value = null, $columns = ['*'])
|
||||
{
|
||||
$model = $this->findByField($field, $value, $columns = ['*']);
|
||||
|
||||
return $model->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find data by field and value
|
||||
*
|
||||
* @param string $field
|
||||
* @param string $value
|
||||
* @param array $columns
|
||||
* @return mixed
|
||||
*/
|
||||
public function findOneWhere(array $where, $columns = ['*'])
|
||||
{
|
||||
$model = $this->findWhere($where, $columns);
|
||||
|
||||
return $model->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find data by id
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $columns
|
||||
* @return mixed
|
||||
*/
|
||||
public function find($id, $columns = ['*'])
|
||||
{
|
||||
$this->applyCriteria();
|
||||
$this->applyScope();
|
||||
$model = $this->model->find($id, $columns);
|
||||
$this->resetModel();
|
||||
|
||||
return $this->parserResult($model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find data by id
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $columns
|
||||
* @return mixed
|
||||
*/
|
||||
public function findOrFail($id, $columns = ['*'])
|
||||
{
|
||||
$this->applyCriteria();
|
||||
$this->applyScope();
|
||||
$model = $this->model->findOrFail($id, $columns);
|
||||
$this->resetModel();
|
||||
|
||||
return $this->parserResult($model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Count results of repository
|
||||
*
|
||||
* @param string $columns
|
||||
* @return int
|
||||
*/
|
||||
public function count(array $where = [], $columns = '*')
|
||||
{
|
||||
$this->applyCriteria();
|
||||
$this->applyScope();
|
||||
|
||||
if ($where) {
|
||||
$this->applyConditions($where);
|
||||
}
|
||||
|
||||
$result = $this->model->count($columns);
|
||||
$this->resetModel();
|
||||
$this->resetScope();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $columns
|
||||
* @return mixed
|
||||
*/
|
||||
public function sum($columns)
|
||||
{
|
||||
$this->applyCriteria();
|
||||
$this->applyScope();
|
||||
|
||||
$sum = $this->model->sum($columns);
|
||||
$this->resetModel();
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $columns
|
||||
* @return mixed
|
||||
*/
|
||||
public function avg($columns)
|
||||
{
|
||||
$this->applyCriteria();
|
||||
$this->applyScope();
|
||||
|
||||
$avg = $this->model->avg($columns);
|
||||
$this->resetModel();
|
||||
|
||||
return $avg;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getModel($data = [])
|
||||
{
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function resetModel()
|
||||
{
|
||||
$this->makeModel();
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
23
packages/Webkul/Core/src/Eloquent/TranslatableModel.php
Executable file
23
packages/Webkul/Core/src/Eloquent/TranslatableModel.php
Executable file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Webkul\Core\Eloquent;
|
||||
|
||||
use Astrotomic\Translatable\Translatable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class TranslatableModel extends Model
|
||||
{
|
||||
use Translatable;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function locale()
|
||||
{
|
||||
if ($this->defaultLocale) {
|
||||
return $this->defaultLocale;
|
||||
}
|
||||
|
||||
return config('translatable.locale') ?: app()->make('translator')->getLocale();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user