Добрый, подскажите, как очищать модель при вызову метода из репозитория?
$pay_from = $this->paySystemService->getByActive('is_from_active', 0, ['*']);
$pay_to = $this->paySystemService->getByActive('is_to_active', 1, ['*']);
если вызываю в таком порядке, и то во $pay_from есть результат, а в $pay_to null
если в таком порядке
$pay_to = $this->paySystemService->getByActive('is_to_active', 1, ['*']);
$pay_from = $this->paySystemService->getByActive('is_from_active', 0, ['*']);
то во $pay_to есть результат, а в $pay_from null
на данный момент у меня вот такой репозиторий
<?php
declare(strict_types=1);
namespace App\Repository\Eloquent;
use App\Repository\Contracts\BaseRepositoryInterface;
use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
abstract class BaseRepository implements BaseRepositoryInterface
{
/**
* Instance of Eloquent Model
*
* @var \Illuminate\Database\Eloquent\Model
*/
protected $model;
/**
* The relations to eager load.
*
* @var
*/
protected $with = [];
/**
* @var \Closure
*/
protected $scopeQuery = null;
public function __construct($model)
{
$this->model = $model;
$this->criteria = new Collection();
}
/**
*
* @return void
*/
public function getModel()
{
return $this->model;
}
/**
* Get all
*
* @param array $columns
* @return void
*/
public function getAll(array $columns = ['*'])
{
$this->applyScope();
if ($this->model instanceof Builder) {
$results = $this->model->get($columns);
} else {
$results = $this->model->all($columns);
}
$this->resetScope();
return $results;
}
/**
* Get with paginate
*
* @param array $columns
* @param integer $perPage
* @return void
*/
public function getPaginate(array $columns = ['*'], int $perPage = 24)
{
$this->applyScope();
$results = $this->model->paginate($perPage, $columns);
$results->appends(app('request')->query());
return $results;
}
/**
*
* @return void
*/
public function first(array $columns = ['*']): ?Model
{
$this->applyScope();
$results = $this->model->first();
return $results;
}
/**
* Create Model
*
* @param array $columns
* @return Model
*/
public function create(array $columns): Model
{
$model = $this->model->create($columns);
return $model;
}
/**
* Undocumented function
*
* @param array $columns
* @return void
*/
public function update(array $columns = ['*']): void
{
$this->applyScope();
$this->model->update($columns);
}
/**
* Update or create a Model with the given ID
*
* @param int $id
* @param \Illuminate\Http\Request $request
* @return void
*/
public function updateOrCreate(int $id, array $columns): void
{
$this->applyScope();
$this->model->updateOrCreate(['id' => $id], $columns);
}
/**
* Find Model by ID
*
* @param integer $id
* @return Model|null
*/
public function findById(int $id, $columns = ['*']): ?Model
{
$this->applyScope();
$model = $this->model->find($id, $columns);
return $model;
}
/**
* Find Model by Column
*
* @param integer $id
* @return Model|null
*/
public function findByUlid(string $id, $columns = ['*']): ?Model
{
$this->applyScope();
$model = $this->model->find($id, $columns);
return $model;
}
/**
* Delete Model by ID
*
* @param integer $id
* @return void
*/
public function delete(int $id): void
{
$this->applyScope();
$this->model->find($id)->delete();
}
/**
* Delete multiple entities by given criteria.
*
* @param array $where
*
* @return int
*/
public function deleteWhere(array $where)
{
$this->applyScope();
$this->applyConditions($where);
$deleted = $this->model->delete();
return $deleted;
}
/**
* Load relations
*
* @param array|string $relations
*
* @return $this
*/
public function with($relations)
{
$this->model = $this->model->with($relations);
return $this;
}
/**
* Find data by multiple fields
*
* @param array $where
* @param array $columns
*
* @return mixed
*/
public function where(array $where)
{
$this->applyScope();
$this->applyConditions($where);
$model = $this->model;
return $model;
}
/**
* Load relation with closure
*
* @param string $relation
* @param closure $closure
*
* @return $this
*/
public function whereHas($relation, $closure)
{
$this->model = $this->model->whereHas($relation, $closure);
return $this;
}
/**
* Add subselect queries to count the relations.
*
* @param mixed $relations
* @return $this
*/
public function withCount($relations)
{
$this->model = $this->model->withCount($relations);
return $this;
}
/**
* Find data by multiple fields
*
* @param array $where
* @param array $columns
*
* @return mixed
*/
public function findWhere(array $where, $columns = ['*'])
{
$this->applyScope();
$this->applyConditions($where);
$model = $this->model->get($columns);
return $model;
}
/**
*
* @param Closure $scope
* @return void
*/
public function scopeQuery(Closure $scope)
{
$this->scopeQuery = $scope;
return $this;
}
/**
* Reset Query Scope
*
* @return $this
*/
public function resetScope()
{
$this->scopeQuery = null;
return $this;
}
/**
* Apply scope in current Query
*
* @return $this
*/
protected function applyScope()
{
if (isset($this->scopeQuery) && is_callable($this->scopeQuery)) {
$callback = $this->scopeQuery;
$this->model = $callback($this->model);
}
return $this;
}
/**
* Applies the given where conditions to the model.
*
* @param array $where
* @return void
*/
protected function applyConditions(array $where)
{
foreach ($where as $field => $value) {
if (is_array($value)) {
list($field, $condition, $val) = $value;
$this->model = $this->model->where($field, $condition, $val);
} else {
$this->model = $this->model->where($field, '=', $value);
}
}
}
}
связываю в провайдере вот так
$this->app->singleton(PaySystemRepositoryInterface::class, function () {
$repository = new PaySystemRepository(
new PaySystem()
);
return $repository;
});