Skip to content

Commit 9500dea

Browse files
committed
✨(repository): 创建时使用新实例
1 parent 0b1a25d commit 9500dea

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/Repositories/BaseRepository.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ public function resetModel()
9595
$this->makeModel();
9696
}
9797

98+
public function lists($column, $key = null)
99+
{
100+
$this->applyConditions();
101+
}
98102

99103
/**
100104
* 根据主键查询
@@ -356,9 +360,10 @@ public function paginate($columns = ['*'], $limit = 0)
356360
*/
357361
public function create(array $attributes)
358362
{
359-
$result = $this->model->create($attributes);
363+
$model = $this->model->newInstance($attributes);
364+
$model->save();
360365
$this->resetModel();
361-
return $result;
366+
return $model;
362367
}
363368

364369
/**
@@ -386,10 +391,13 @@ public function createBatch(array $attributes)
386391
*/
387392
public function updateById(array $attributes, int $id)
388393
{
389-
$model = $this->model->find($id);
390-
$result = $model->update($attributes);
394+
$model = $this->model->findOrFail($id);
395+
396+
$model->fill($attributes);
397+
$model->save();
398+
391399
$this->resetModel();
392-
return $result;
400+
return $model;
393401
}
394402

395403
/**
@@ -498,8 +506,8 @@ public function updateBatch(array $multipleData = []): bool
498506
public function deleteById(int $id)
499507
{
500508
$model = $this->find($id);
501-
$result = $model->delete();
502509
$this->resetModel();
510+
$result = $model->delete();
503511
return $result;
504512

505513
}
@@ -885,7 +893,7 @@ public function commit()
885893
DB::commit();
886894
}
887895

888-
896+
889897
/**
890898
* 打印sql语句
891899
* @param Closure $callback
@@ -904,4 +912,14 @@ public function getQuerySql(Closure $callback, string $tableName = '')
904912
}
905913
}
906914

915+
/**
916+
* @param $method
917+
* @param $arguments
918+
* @return mixed
919+
*/
920+
public function __call($method, $arguments)
921+
{
922+
return call_user_func_array([$this->model, $method], $arguments);
923+
}
924+
907925
}

0 commit comments

Comments
 (0)