UserRepositoryEloquent.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Repositories\Eloquent;
  11. use App\Contracts\Repositories\UserRepository;
  12. use App\Repositories\Criteria\RequestCriteria;
  13. use App\Repositories\Models\User;
  14. use App\Repositories\Validators\UserValidator;
  15. use Illuminate\Support\Facades\Hash;
  16. /**
  17. * Class UserRepositoryEloquent.
  18. */
  19. class UserRepositoryEloquent extends BaseRepository implements UserRepository
  20. {
  21. protected $fieldSearchable = [
  22. 'name' => 'like',
  23. 'email', // Default Condition "="
  24. ];
  25. /**
  26. * Specify Model class name.
  27. *
  28. * @return string
  29. */
  30. public function model()
  31. {
  32. return User::class;
  33. }
  34. /**
  35. * Specify Validator class name.
  36. *
  37. * @return mixed
  38. */
  39. public function validator()
  40. {
  41. return UserValidator::class;
  42. }
  43. /**
  44. * Boot up the repository, pushing criteria.
  45. *
  46. * @throws \Prettus\Repository\Exceptions\RepositoryException
  47. */
  48. public function boot()
  49. {
  50. $this->pushCriteria(app(RequestCriteria::class));
  51. }
  52. public function insertUser($attributes)
  53. {
  54. $this->model->name = $attributes['name'];
  55. $this->model->email = $attributes['email'];
  56. $this->model->password = Hash::make($attributes['password']);
  57. $this->model->saveOrFail();
  58. return $this->model;
  59. }
  60. }