User.php 810 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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 getFullNameAttribute()
  13. {
  14. if (! $this->profile) {
  15. return;
  16. }
  17. return "{$this->profile['first_name']} {$this->profile['last_name']}";
  18. }
  19. public function getPositionAttribute()
  20. {
  21. if (! $this->profile) {
  22. return;
  23. }
  24. return "{$this->profile->latitude} {$this->profile->longitude}";
  25. }
  26. public function tags()
  27. {
  28. return $this->belongsToMany(Tag::class, 'test_user_tags', 'user_id', 'tag_id');
  29. }
  30. }