自动更新一对多关系 [英] updating one to many relationship automatically

查看:196
本文介绍了自动更新一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表(notification和alertFrequency),它们分别是models.有一对多的关系.有用.当我尝试自动更新时,这里就是问题所在.简而言之,我的模型通知是:

I have two tables(notification and alertFrequency) with their respective models. There is a one-to-many relationship. It works. when i try to update it automatically, here is where I get the issues. In short my model notification is:

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();
    }
}

它返回时间戳.

我要在枪口控制器中完成的工作是用notification_id(alertFrequency表中的字段)更新created_at(alertFrequency表中的字段).代码如下

What I'am trying to accomplish in the guzzle controller is to update the created_at(field in alertFrequency table) with notification_id(field in alertFrequency table). the code is as follows

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表?非常感谢您的帮助!

till function update, I am able to var_dump() the value and is working correctly. but I don't know how to call it in the status function so that it will automatically update the alerFrequency table? would appreciate your help!

推荐答案

如果我理解正确,则想从status()调用updateStatus()方法.您不能这样做,因为updateStatus()期望AlertFrequency的实例作为参数.这就是问题.

If I understand correctly you want to call updateStatus() method from status(). You cannot do it as updateStatus() expect instance of AlertFrequency as an argument. This is the issue.

如果是这样,请像这样稍微更改您的``updateStatus()方法,这样您就不需要传递AlertFrequency作为参数了.您可以在updateStatus()方法中使用app()方法来解决它.

If so, slightly change your ``updateStatus() method like this so you don't need to pass AlertFrequency as an argument. You can resolve it using app() method inside updateStatus() method.

根据您的评论,我进行了一些更改.

With you comment I've made some changes.

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方法之前,请务必查看属性批量分配.

这篇关于自动更新一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆