From TonyM, 6 Years ago, written in PHP.
Embed
  1.     public function devices()
  2.     {
  3.         if ($this->hasGlobalRead()) {
  4.             $instance = $this->newRelatedInstance('App\Models\Device');
  5.             return new HasAll($instance);
  6.         } else {
  7.             return $this->belongsToMany('App\Models\Device', 'devices_perms', 'user_id', 'device_id');
  8.         }
  9.     }