自动更新一对多关系

时间:2017-01-16 13:40:35

标签: php laravel

我有两个表(notification和alertFrequency)及其各自的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控制器中尝试完成的是使用created_at更新alertFrequencynotification_id表中的字段)(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表?非常感谢你的帮助!

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您想从updateStatus()拨打status()方法。你不能这样做updateStatus()期望AlertFrequency的实例作为参数。这就是问题所在。

如果是这样,稍微更改你的``updateStatus()方法,这样你就不需要传递AlertFrequency作为参数。您可以使用app()方法中的updateStatus()方法解决此问题。

随着你发表评论,我做了一些改变。

private function updateStatus(Notification $notification, Status $status)
{
     $alert = app(AlertFrequency::class);
    // Rest of your code here

    if ($check || $elapsed_time >= $check_frequency) {
        return $this->getAlertFrequency();
    }
    return null;
}

public function status()
{
    $notifications = Notification::where('active', 1)->get();
    $status = Status::where('name', 'health')->first();
    foreach ($notifications as $notification) {
        $frequency = $this->updateStatus($notification, $status);
        if (!empty($frequency)) {
            $notification->alertFrequencies()->create([
                'notification_id' => $frequency
            ]);
        }
    }
}
  

在使用create方法之前,请务必查看attribute mass assignment上的文档。