Location.php 982 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace App\Models;
  3. use App\Classes\Pterodactyl;
  4. use Exception;
  5. use Illuminate\Database\Eloquent\Factories\HasFactory;
  6. use Illuminate\Database\Eloquent\Model;
  7. class Location extends Model
  8. {
  9. use HasFactory;
  10. public $incrementing = false;
  11. public $guarded = [];
  12. public function nodes(){
  13. return $this->hasMany(Node::class , 'location_id' , 'id');
  14. }
  15. /**
  16. * Sync locations with pterodactyl panel
  17. * @throws Exception
  18. */
  19. public static function syncLocations(){
  20. $locations = Pterodactyl::getLocations();
  21. $locations = array_map(function($val) {
  22. return array(
  23. 'id' => $val['attributes']['id'],
  24. 'name' => $val['attributes']['short'],
  25. 'description' => $val['attributes']['long']
  26. );
  27. }, $locations);
  28. foreach ($locations as $location) {
  29. self::firstOrCreate(['id' => $location['id']] , $location);
  30. }
  31. }
  32. }