84 lines
1.7 KiB
PHP
84 lines
1.7 KiB
PHP
<?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\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Geofence extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'traccar_geofence_id',
|
|
'traccar_id',
|
|
'name',
|
|
'description',
|
|
'area',
|
|
'coordinates',
|
|
'type',
|
|
'latitude',
|
|
'longitude',
|
|
'radius',
|
|
'attributes',
|
|
'calendar_id',
|
|
'is_active',
|
|
];
|
|
|
|
protected $casts = [
|
|
'latitude' => 'decimal:8',
|
|
'longitude' => 'decimal:8',
|
|
'radius' => 'decimal:2',
|
|
'attributes' => 'array',
|
|
'is_active' => 'boolean',
|
|
];
|
|
|
|
/**
|
|
* Get the user that owns the geofence
|
|
*/
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
/**
|
|
* Get devices assigned to this geofence
|
|
*/
|
|
public function devices(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Device::class, 'device_geofences');
|
|
}
|
|
|
|
/**
|
|
* Get events related to this geofence
|
|
*/
|
|
public function events(): HasMany
|
|
{
|
|
return $this->hasMany(Event::class);
|
|
}
|
|
|
|
/**
|
|
* Scope for active geofences
|
|
*/
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where('is_active', true);
|
|
}
|
|
|
|
/**
|
|
* Get geofence type color
|
|
*/
|
|
public function getTypeColor(): string
|
|
{
|
|
return match ($this->type) {
|
|
'circle' => 'blue',
|
|
'polygon' => 'green',
|
|
default => 'gray'
|
|
};
|
|
}
|
|
}
|