我有一个名为通知的模型。该模型负责从数据库中获取数据。它有一个静态方法,可以将从数据库中检索到的数据更改为字符串值。如果存储的数据是200,则将其更改为“up”,否则将其更改为“down”。我在我的刀片页面中调用了此功能,以根据其状态显示所有网址。也就是说,它会显示“向上”,“向下”和“未激活”的网址。由于某种原因,它无法显示网站,对于关闭和没有活跃的网站,它做得很好。
这是我的模型:通知模型
<?php
namespace App;
use App\Status;
use App\Notification;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
protected $fillable = ['id','website_url','email','slack_channel','check_frequency','alert_frequency','speed_frequency','active'];
public function statuses(){
return $this->belongsToMany('App\Status')->withPivot('values')->withTimestamps();
}
public function status($s_type)
{
if (!in_array($s_type, ['speed', 'health'])){
return false;
}
$o_status = Status::where('name', strval($s_type))->first();
$o_response = $this->statuses()->where('status_id', $o_status->id)
->select('values', 'created_at')->orderBy('created_at','DESC')->first();
if($o_response === null){
return false;
}
return [
'value' => $o_response->values,
'timestamp' => $o_response->created_at
];
}
public function history($s_type)
{
if (!in_array($s_type, ['speed', 'health'])){
return false;
}
$o_status = Status::where('name', strval($s_type))->first();
$o_response = $this->statuses()->where('status_id', $o_status->id)
->select('values', 'created_at')->orderBy('created_at','DESC')->get();
if($o_response === null || $o_response->count() === 0){
return false;
}
$a_ret = [];
$last = null;
foreach ($o_response as $o_status) {
if($last != $o_status->values) {
$a_ret[] = [
'value' => $o_status->values,
'timestamp' => $o_status->created_at
];
$last = $o_status->values;
}
}
return $a_ret;
}
public static function health($resCode)
{
return intval($resCode) === 200 ? 'up' : 'down';
}
}
这是我的刀片页面
<tbody>
@foreach($notes as $notification)
<?php $notificationHealth = $notification->status('health'); ?>
<?php $notificationSpeed = $notification->status('speed'); ?>
@if ((is_null($active) || $notification->active == $active) && (empty($health) || ($health === 'down' && empty($notificationHealth['value'])) || $notificationHealth['value'] === $health))
<tr>
<td> {{ $notification->website_url }} </td>
@if($notificationHealth !== false)
@if(\App\Notification::health($notificationHealth['value']) == 'up')
<td> {{ \App\Notification::health($notificationHealth['value']) }} <span class="up"></span><br><span class="timestamp_green">{{ $notificationHealth['timestamp'] }}</span></td>.
@elseif(\App\Notification::health($notificationHealth['value']) == 'down')
<td> {{ \App\Notification::health($notificationHealth['value']) }} <span class="down"></span><br><span class="timestamp_red">{{ $notificationHealth['timestamp'] }}</span></td>
@else
<td> {{ \App\Notification::health($notificationHealth['value']) }} <span class="unsigned"></span><br><span class="timestamp_unsigned">{{ $notificationHealth['timestamp'] }}</span></td>
@endif
@else
<td> N/A <span class="unsigned"></span></td>
@endif
帮助提供帮助!