Node.php 1.3 KB

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