I'm working inside a Laravel 8 application used as a backend to a front-end. I have 3 models:
Domain
Dns
(linked toDomain
usingbelongsTo
)DnsCheck
(linked toDns
usingbelongsTo
)
The general idea is that the Domain
model holds a website such as example.com, and Dns
simply contains metadata about how the DnsCheck
's should be performed, and, because the number of DNS types per domain is unknown, DnsCheck
has a type
column for instance: a or aaaa as there may be more than one record per domain.
Ultimatly, there could be as many as 15 DnsCheck
entries, and the Dns
model contains an interval
column which dictactes how often the DNS of a domain should be checked, so there could be 13 entries generated every hour for instance.
My issue I'm finding now is I'd like to show the end user all of the latest "checks" performed on their domain's DNS and return it as an array that I can loop over, but to the best of my knowledge, on my Dns
model the latestOfMany()
method would simply give me one result?
How can I get the latest records, I have a checked_at
column on my DnsCheck
model, I'll attach a screenshot.
Dns
model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Dns extends Model
{
use HasFactory;
/**
* Indicates if the model's ID is auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'dns';
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'last_notified_at' => 'datetime',
'last_checked_at' => 'datetime',
'disabled_at' => 'datetime',
// record types that we can monitor
'a_record_is_enabled' => 'boolean',
'aaaa_record_is_enabled' => 'boolean',
'caa_record_is_enabled' => 'boolean',
'cname_record_is_enabled' => 'boolean',
'mx_record_is_enabled' => 'boolean',
'ns_record_is_enabled' => 'boolean',
'soa_record_is_enabled' => 'boolean',
'srv_record_is_enabled' => 'boolean',
'txt_record_is_enabled' => 'boolean',
'ptr_record_is_enabled' => 'boolean'
];
/**
* Get the dns checks that this dns has
*/
public function dns_checks()
{
return $this->hasMany(DnsCheck::class);
}
/**
* Get the latest dns check
*/
public function latest_dns_check()
{
return $this->hasOne(DnsCheck::class)->latestOfMany();
}
/**
* Get the domain that owns this dns
*/
public function domain()
{
return $this->belongsTo(Domain::class);
}
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted()
{
static::deleted(function ($model) {
$model->dns_checks()->delete();
});
}
}
DnsCheck
model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class DnsCheck extends Model
{
use HasFactory;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'dns_checks';
/**
* Get the dns that owns this dns check
*/
public function certificate()
{
return $this->belongsTo(Dns::class);
}
}