User.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Models;
  3. // use Illuminate\Contracts\Auth\MustVerifyEmail;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Foundation\Auth\User as Authenticatable;
  6. use Illuminate\Notifications\Notifiable;
  7. use Laravel\Sanctum\HasApiTokens;
  8. use Itstructure\LaRbac\Interfaces\RbacUserInterface;
  9. use Itstructure\LaRbac\Traits\Administrable;
  10. use Illuminate\Support\Facades\Validator;
  11. class User extends Authenticatable implements RbacUserInterface
  12. {
  13. use HasApiTokens, HasFactory, Notifiable, Administrable;
  14. /**
  15. * The attributes that are mass assignable.
  16. *
  17. * @var array<int, string>
  18. */
  19. protected $fillable = [
  20. 'name',
  21. 'email',
  22. 'password',
  23. 'user_type',
  24. 'phone_number',
  25. 'roles'
  26. ];
  27. /**
  28. * The attributes that should be hidden for serialization.
  29. *
  30. * @var array<int, string>
  31. */
  32. protected $hidden = [
  33. 'password',
  34. 'remember_token',
  35. ];
  36. /**
  37. * The attributes that should be cast.
  38. *
  39. * @var array<string, string>
  40. */
  41. protected $casts = [
  42. 'email_verified_at' => 'datetime',
  43. ];
  44. public function hasRole($hasRole)
  45. {
  46. foreach ($this->roles()->get() as $role) {
  47. if($role->name == $hasRole){
  48. return true;
  49. }else{
  50. return false;
  51. }
  52. }
  53. }
  54. public function userData(){
  55. return $this->hasOne('App\Models\UserData', 'user_id');
  56. }
  57. public function userDocuments(){
  58. return $this->hasMany('App\Models\UserDocuments', 'user_id');
  59. }
  60. public function signupValidate($request){
  61. $validate = Validator::make($request->all(), [
  62. 'email' => 'required|email',
  63. 'first_name' => 'required',
  64. 'last_name' => 'required',
  65. 'phone_number' => 'required',
  66. 'user_type' => 'required',
  67. ]);
  68. if($validate->fails()){
  69. return response()->json([
  70. 'status' => false,
  71. 'message' => 'Validation errors in your request',
  72. 'errors' => $validate->errors()
  73. ], 400);
  74. }
  75. $postData = $request->all();
  76. $userPhone = User::with(['UserData'])->whereRelation('UserData', 'phone_number', '=', $postData['phone_number'])->first();
  77. if($userPhone){
  78. return response()->json([
  79. 'status' => false,
  80. 'message' => 'Validation errors in your request',
  81. 'errors' => 'Phone number already exist'
  82. ], 400);
  83. }
  84. $userEmail = User::with(['UserData'])->where('email', '=', $postData['email'])->first();
  85. if($userEmail){
  86. return response()->json([
  87. 'status' => false,
  88. 'message' => 'Validation errors in your request',
  89. 'errors' => 'Email already exist'
  90. ], 400);
  91. }
  92. return response()->json([
  93. 'status' => true,
  94. 'message' => 'sucess',
  95. 'errors' => ''
  96. ], 200);
  97. }
  98. }