mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Link Model (#15611)
* Link Model and relationships * Some uses of the new model
This commit is contained in:
@@ -26,13 +26,16 @@
|
||||
namespace App\Http\Controllers\Device\Tabs;
|
||||
|
||||
use App\Models\Device;
|
||||
use App\Models\Link;
|
||||
use LibreNMS\Interfaces\UI\DeviceTab;
|
||||
|
||||
class NeighboursController implements DeviceTab
|
||||
{
|
||||
public function visible(Device $device): bool
|
||||
{
|
||||
return \DB::table('links')->where('local_device_id', $device->device_id)->exists();
|
||||
return Link::where('local_device_id', $device->device_id)
|
||||
->orWhere('remote_device_id', $device->device_id)
|
||||
->exists();
|
||||
}
|
||||
|
||||
public function slug(): string
|
||||
|
@@ -766,6 +766,21 @@ class Device extends BaseModel
|
||||
return $this->hasMany(\App\Models\IsisAdjacency::class, 'device_id', 'device_id');
|
||||
}
|
||||
|
||||
public function links(): HasMany
|
||||
{
|
||||
return $this->hasMany(\App\Models\Link::class, 'local_device_id');
|
||||
}
|
||||
|
||||
public function remoteLinks(): HasMany
|
||||
{
|
||||
return $this->hasMany(\App\Models\Link::class, 'remote_device_id');
|
||||
}
|
||||
|
||||
public function allLinks(): \Illuminate\Support\Collection
|
||||
{
|
||||
return $this->links->merge($this->remoteLinks);
|
||||
}
|
||||
|
||||
public function location(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\App\Models\Location::class, 'location_id', 'id');
|
||||
|
37
app/Models/Link.php
Normal file
37
app/Models/Link.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
|
||||
class Link extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
// ---- Define Relationships ----
|
||||
|
||||
public function device(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\App\Models\Device::class, 'local_device_id', 'device_id');
|
||||
}
|
||||
|
||||
public function port(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\App\Models\Port::class, 'local_port_id', 'port_id');
|
||||
}
|
||||
|
||||
public function remoteDevice(): HasOne
|
||||
{
|
||||
return $this->hasOne(\App\Models\Device::class, 'device_id', 'remote_device_id');
|
||||
}
|
||||
|
||||
public function remotePort(): HasOne
|
||||
{
|
||||
return $this->hasOne(\App\Models\Port::class, 'port_id', 'remote_port_id');
|
||||
}
|
||||
}
|
@@ -42,11 +42,12 @@ class Port extends DeviceRelatedModel
|
||||
$port->statistics()->delete();
|
||||
$port->stp()->delete();
|
||||
$port->vlans()->delete();
|
||||
$port->links()->delete();
|
||||
$port->remoteLinks()->delete();
|
||||
|
||||
// dont have relationships yet
|
||||
DB::table('juniAtmVp')->where('port_id', $port->port_id)->delete();
|
||||
DB::table('ports_perms')->where('port_id', $port->port_id)->delete();
|
||||
DB::table('links')->where('local_port_id', $port->port_id)->orWhere('remote_port_id', $port->port_id)->delete();
|
||||
DB::table('ports_stack')->where('port_id_low', $port->port_id)->orWhere('port_id_high', $port->port_id)->delete();
|
||||
|
||||
\Rrd::purge($port->device?->hostname, \Rrd::portName($port->port_id)); // purge all port rrd files
|
||||
@@ -310,6 +311,21 @@ class Port extends DeviceRelatedModel
|
||||
return $this->hasMany(\App\Models\Ipv6Address::class, 'port_id');
|
||||
}
|
||||
|
||||
public function links(): HasMany
|
||||
{
|
||||
return $this->hasMany(\App\Models\Link::class, 'local_port_id');
|
||||
}
|
||||
|
||||
public function remoteLinks(): HasMany
|
||||
{
|
||||
return $this->hasMany(\App\Models\Link::class, 'remote_port_id');
|
||||
}
|
||||
|
||||
public function allLinks(): \Illuminate\Support\Collection
|
||||
{
|
||||
return $this->links->merge($this->remoteLinks);
|
||||
}
|
||||
|
||||
public function macAccounting(): HasMany
|
||||
{
|
||||
return $this->hasMany(MacAccounting::class, 'port_id');
|
||||
|
Reference in New Issue
Block a user