heizung/app/Models/Device.php

16 lines
781 B
PHP
Raw Normal View History

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Device extends Model {
protected $fillable = ['tenant_id','name','slug','type','mqtt_topic_prefix','mqtt_username','timezone','settings','last_seen_at'];
protected $casts = ['settings'=>'array','last_seen_at'=>'datetime'];
public function tenant(): BelongsTo { return $this->belongsTo(Tenant::class); }
public function readings(): HasMany { return $this->hasMany(Reading::class); }
public function events(): HasMany { return $this->hasMany(Event::class); }
public function latestReading(string $metric) {
return $this->readings()->where('metric',$metric)->latest('recorded_at')->first();
}
}