User.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App;
  3. use Illuminate\Foundation\Auth\User as Authenticatable;
  4. use Illuminate\Notifications\Notifiable;
  5. class User extends Authenticatable
  6. {
  7. use Notifiable;
  8. /**
  9. * The attributes that are mass assignable.
  10. *
  11. * @var array
  12. */
  13. protected $fillable = [
  14. 'nickname', 'email', 'password', 'role_id'
  15. ];
  16. /**
  17. * The attributes that should be hidden for arrays.
  18. *
  19. * @var array
  20. */
  21. protected $hidden = [
  22. 'password', 'remember_token',
  23. ];
  24. public function role()
  25. {
  26. return $this->belongsTo('App\Models\Role', 'role_id', 'id');
  27. // return $this->hasOne('App\Models\Role', 'id', 'role_id');
  28. }
  29. public function items()
  30. {
  31. return $this->hasMany('App\Models\Item');
  32. }
  33. public function offersToUser()
  34. {
  35. return $this->hasManyThrough('App\Models\Offer', 'App\Models\Item', 'user_id', 'item_id');
  36. }
  37. public function offersFromUser()
  38. {
  39. return $this->hasManyThrough('App\Models\Offer', 'App\Models\Item', 'user_id', 'item_offer_id');
  40. }
  41. public function __get($key)
  42. {
  43. if ($key === 'name') return $this->attributes['nickname'];
  44. if ($key === 'role') {
  45. return $this->role()->first()->name;
  46. };
  47. return parent::__get($key);
  48. }
  49. public function isAdmin()
  50. {
  51. return ($this->role === 'admin');
  52. // return ($this->role()->first()->name === 'admin');
  53. }
  54. }