UserRepository.php 886 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\User;
  4. class UserRepository extends ResourceRepository
  5. {
  6. //protected $user;
  7. public function __construct(User $user)
  8. {
  9. $this->user = $user;
  10. }
  11. private function save(User $user, Array $inputs)
  12. {
  13. $user->name = $inputs['name'];
  14. $user->email = $inputs['email'];
  15. $user->admin = isset($inputs['admin']);
  16. $user->save();
  17. }
  18. public function getPaginate($n)
  19. {
  20. return $this->user->paginate($n);
  21. }
  22. public function store(Array $inputs)
  23. {
  24. $user = new $this->user;
  25. $user->password = bcrypt($inputs['password']);
  26. $this->save($user, $inputs);
  27. return $user;
  28. }
  29. public function getById($id)
  30. {
  31. return $this->user->findOrFail($id);
  32. }
  33. public function update($id, Array $inputs)
  34. {
  35. $this->save($this->getById($id), $inputs);
  36. }
  37. public function destroy($id)
  38. {
  39. $this->getById($id)->delete();
  40. }
  41. }