User.php 1019 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace Tests\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class User extends Model
  5. {
  6. protected $table = 'test_users';
  7. protected $appends = ['full_name', 'position'];
  8. public function profile()
  9. {
  10. return $this->hasOne(Profile::class, 'user_id');
  11. }
  12. public function user_profile()
  13. {
  14. return $this->hasOne(Profile::class, 'user_id');
  15. }
  16. public function userProfile()
  17. {
  18. return $this->hasOne(Profile::class, 'user_id');
  19. }
  20. public function getFullNameAttribute()
  21. {
  22. if (! $this->profile) {
  23. return;
  24. }
  25. return "{$this->profile['first_name']} {$this->profile['last_name']}";
  26. }
  27. public function getPositionAttribute()
  28. {
  29. if (! $this->profile) {
  30. return;
  31. }
  32. return "{$this->profile->latitude} {$this->profile->longitude}";
  33. }
  34. public function tags()
  35. {
  36. return $this->belongsToMany(Tag::class, 'test_user_tags', 'user_id', 'tag_id');
  37. }
  38. }