【发布时间】:2017-01-16 23:02:15
【问题描述】:
我有两个表(通知和警报频率),它们各自的models。存在一对多的关系。有用。当我尝试自动更新它时,这就是我遇到问题的地方。简而言之,我的模型通知是:
class Notification extends Model
{
public function alertFrequencies()
{
return $this->hasMany('App\AlertFrequency');
}
public function alert()
{
$alert_frequency = AlertFrequency::with('notification')
->orderBy('created_at', 'desc')->select('created_at')->first();
if ($alert_frequency == null) {
return false;
}
return $alert_frequency->created_at->toDateTimeString();
}
}
它返回时间戳。
我想要在 guzzle 控制器中完成的是用notification_id(alertFrequency 表中的字段)更新created_at(alertFrequency 表中的字段)。代码如下
public function status()
{
$notifications = Notification::where('active', 1)->get();
$status = Status::where('name', 'health')->first();
foreach ($notifications as $notification) {
$this->updateStatus($notification, $status);
}
}
private function updateStatus(Notification $notification, Status $status, AlertFrequency $alert)
{
$status_health = $notification->status('health');
$check = empty($status_health['timestamp']);
$elapsed_time = $check ? 10000 : \Carbon\Carbon::parse($status_health['timestamp'])->diffInMinutes();
$check_frequency = $this->getCheckFrequency($notification);
if ($check || $elapsed_time >= $check_frequency) {
$resCode = $this->getStatusCode($notification->website_url);
$this->addStatusToNotification($notification, $status, $resCode);
$this->sendNotification(
$notification,
$status_health,
$this->getAlertFrequency($alert),
$resCode
);
/*working right for alert*/
var_dump($this->getAlertFrequency);
}
}
private function getAlertFrequency(AlertFrequency $notification)
{
if ($notification->alert() == null) {
return false;
}
return $notification->alert();
}
直到函数更新,我能够var_dump() 值并且工作正常。但我不知道如何在status 函数中调用它,以便它会自动更新alerFrequency 表?非常感谢您的帮助!
【问题讨论】:
-
alerFrequency表的结构是什么 -
这是一个简单的模型,具有多属关系::::public function notification(){ return $this->belongsTo('App\Notification'); }