Node.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  8. class Node extends Model
  9. {
  10. use HasFactory;
  11. public $incrementing = false;
  12. public $guarded = [];
  13. /**
  14. * @return BelongsTo
  15. */
  16. public function location(): BelongsTo
  17. {
  18. return $this->belongsTo(Location::class);
  19. }
  20. /**
  21. * @throws Exception
  22. */
  23. public static function syncNodes(){
  24. Location::syncLocations();
  25. $nodes = Pterodactyl::getNodes();
  26. $nodes = array_map(function($node) {
  27. return array(
  28. 'id' => $node['attributes']['id'],
  29. 'location_id' => $node['attributes']['location_id'],
  30. 'name' => $node['attributes']['name'],
  31. 'description' => $node['attributes']['description'],
  32. 'disabled' => '1'
  33. );
  34. }, $nodes);
  35. foreach ($nodes as $node) {
  36. self::firstOrCreate(['id' => $node['id']] , $node);
  37. }
  38. }
  39. }