Eloquent.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\framework\Repository\Source;
  3. use app\framework\Repository\RepositorySourceInterface;
  4. use Illuminate\Database\Eloquent\Model;
  5. class Eloquent implements RepositorySourceInterface
  6. {
  7. /**
  8. * @var Model
  9. */
  10. private $model;
  11. /**
  12. * @var array
  13. */
  14. private $excludeFields;
  15. public function __construct(Model $model, $excludeFields = [])
  16. {
  17. $this->model = $model;
  18. $this->excludeFields = $excludeFields;
  19. }
  20. public function getBuilder()
  21. {
  22. $builder = $this->model;
  23. if ($this->excludeFields) {
  24. $builder = $builder->exclude($this->excludeFields);
  25. }
  26. return $builder;
  27. }
  28. public function find($id)
  29. {
  30. return $this->model->find($id);
  31. }
  32. public function findManyBy($key, $ids)
  33. {
  34. return $this->getBuilder()->getQuery()->whereIn($key, $ids)->get();
  35. }
  36. public function findBy($key, $id)
  37. {
  38. return $this->model->where($key, $id)->first($id);
  39. }
  40. public function all()
  41. {
  42. return $this->getBuilder()->getQuery()->get();
  43. }
  44. public function findMany($ids)
  45. {
  46. return $this->getBuilder()->getQuery()->whereIn($this->model->getKeyName(), $ids)->get();
  47. }
  48. public function save($id, $data)
  49. {
  50. return $this->getBuilder()->where($this->model->getKeyName(), $id)->update($data);
  51. }
  52. public function saveMany($data)
  53. {
  54. return true;
  55. }
  56. public function saveBy($key, $id, $data)
  57. {
  58. return true;
  59. }
  60. public function saveManyBy($key, $data)
  61. {
  62. return true;
  63. }
  64. public function saveAll($data)
  65. {
  66. return true;
  67. }
  68. public function flush()
  69. {
  70. return true;
  71. }
  72. }