Skip to content

Commit

Permalink
✨(repository): 创建时使用新实例
Browse files Browse the repository at this point in the history
  • Loading branch information
Joycezhangw committed Feb 25, 2024
1 parent 0b1a25d commit 9500dea
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/Repositories/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public function resetModel()
$this->makeModel();
}

public function lists($column, $key = null)
{
$this->applyConditions();
}

/**
* 根据主键查询
Expand Down Expand Up @@ -356,9 +360,10 @@ public function paginate($columns = ['*'], $limit = 0)
*/
public function create(array $attributes)
{
$result = $this->model->create($attributes);
$model = $this->model->newInstance($attributes);
$model->save();
$this->resetModel();
return $result;
return $model;
}

/**
Expand Down Expand Up @@ -386,10 +391,13 @@ public function createBatch(array $attributes)
*/
public function updateById(array $attributes, int $id)
{
$model = $this->model->find($id);
$result = $model->update($attributes);
$model = $this->model->findOrFail($id);

$model->fill($attributes);
$model->save();

$this->resetModel();
return $result;
return $model;
}

/**
Expand Down Expand Up @@ -498,8 +506,8 @@ public function updateBatch(array $multipleData = []): bool
public function deleteById(int $id)
{
$model = $this->find($id);
$result = $model->delete();
$this->resetModel();
$result = $model->delete();
return $result;

}
Expand Down Expand Up @@ -885,7 +893,7 @@ public function commit()
DB::commit();
}


/**
* 打印sql语句
* @param Closure $callback
Expand All @@ -904,4 +912,14 @@ public function getQuerySql(Closure $callback, string $tableName = '')
}
}

/**
* @param $method
* @param $arguments
* @return mixed
*/
public function __call($method, $arguments)
{
return call_user_func_array([$this->model, $method], $arguments);
}

}

0 comments on commit 9500dea

Please sign in to comment.