DistAdminDistributor.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. class DistAdminDistributor extends Model
  6. {
  7. protected $table = 'dist_admin_distributor';
  8. // 关联关系:一个 Distributor 可能有多个 AppearancePublish
  9. public function appearancePublishes()
  10. {
  11. return $this->hasMany(DistAppearancePublishList::class, 'dist_id', 'id');
  12. }
  13. /**
  14. * 关联到 DistAppearance
  15. */
  16. public function appearance()
  17. {
  18. return $this->belongsTo(DistAppearance::class, 'appearance_id', 'id');
  19. }
  20. /**
  21. * 根据域名查找 Distributor 并加载关联数据
  22. *
  23. * @param string $domain
  24. * @return self|null
  25. */
  26. public static function findByDomainWithAppearances(string $domain)
  27. {
  28. return self::with('appearancePublishes') // 预加载关联数据
  29. ->where('custom_domain', $domain)
  30. ->orWhere('secondary_domain', $domain)
  31. ->first();
  32. }
  33. /**
  34. * 根据域名查找 Distributor(不加载关联数据)
  35. *
  36. * @param string $domain
  37. * @return self|null
  38. */
  39. public static function findByDomain(string $domain)
  40. {
  41. return self::where('custom_domain', $domain)
  42. ->orWhere('secondary_domain', $domain)
  43. ->first(); // 只查找当前模型数据
  44. }
  45. }