gps_system/app/Models/Geofence.php
sackey 6b878bb0a0
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
Initial commit
2025-09-12 16:19:56 +00:00

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'
};
}
}