Laravel一对多关系插入数据

时间:2017-01-16 16:15:14

标签: php laravel

我有两张表notificationalerFrequency。他们分别有一对多的关系。 notification_idalerFrequency表中的外键。两个表都有模型。现在我要做的是,如果在alertFrequency表中添加了网站,则自动将数据插入notification表。这是notification

<?php
namespace App;
use App\Status;
use App\Notification;
use App\AlertFrequency;

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 alertFrequencies(){
        return $this->hasMany('App\AlertFrequency');
    }
    public  function alert(){
        $alert_timestamp = AlertFrequency::with('notification')->orderBy('created_at','desc')->select('created_at')->first();
        $alert_timestamp=$alert_timestamp->created_at->toDateTimeString();
        if($alert_timestamp==null){
            return false;
        }       
            return $alert_timestamp;    }

并且在guzzle控制器中,我使用了3个函数:add函数将新的alertFrequency添加到表中(根本不工作)和我调用的它位于sendnotification函数中,因此如果是时候发送通知,它会在created_at表中添加新的alerFrequency。这是guzzle控制器

<?php
namespace App\Http\Controllers;

use \GuzzleHttp\Client;
use App\Utilities\Reporter;
use GuzzleHttp\Exception\ClientException;
use App\Notification;
use App\Status;
use App\Setting;
use Carbon;
use App\AlertFrequency;
class GuzzleController extends Controller
{
    private $default_check_frequency;

    protected $client;

    protected $reporter;



    public function __construct()
    {
        $this->client = new Client();

        $this->reporter = new Reporter;

        $this->default_check_frequency = Setting::defaultCheckFrequency();
    }

    private function addStatusToNotification(Notification $notification, Status $status, $resCode)
    {
        $notification->statuses()->attach($status, [
            'values' => strval($resCode)
        ]);
    }
    /*function to add new time stamp into the alertFrequency table*/
    private function add(Notification $notification, AlertFrequency $alert){
        $notification->alertFrequency()->save();
    }

    private function report(Notification $notification, $resCode)
    {
        if(empty($resCode)){
            $resCode = "no response found";
        }

        $status = Notification::health($resCode);
        $this->reporter->slack($notification->website_url . ':' . '  is '. $status . ' this is the status code!' . ' @- ' .$resCode, $notification->slack_channel);
        $this->reporter->mail($notification->email,$notification->website_url.' is '. $status . ' this is the status Code: '. $resCode);
    }

    private function sendNotification(Notification $notification, $status_health, $alert_frequency, $resCode,$alert)
    {
        echo "elpse time alert";
        var_dump(\Carbon\Carbon::parse($alert)->diffInMinutes());
        // If this is the first time we check, OR if the status changed from up to down and vice versa, notify!!!
        if (empty($status_health['timestamp']) || Notification::health($resCode) <> Notification::health($status_health['value'])){
            $this->report($notification,$resCode);
            return;
        }

        // If the website is (still) down and the alert frequency is exceeded, notify!!!
        if(Notification::health($resCode) === 'down' && \Carbon\Carbon::parse($alert)->diffInMinutes() >= $alert_frequency){
            $this->report($notification,$resCode);
            $this->add($notification,$alert);
        }
    }


    public function status()
    {
        $notifications = Notification::where('active', 1)->get();
        //$alert = AlertFrequency::
        $status = Status::where('name', 'health')->first();

        foreach ($notifications as $notification) {
             $frequency = $this->updateStatus($notification, $status);
        if (!empty($frequency)) {
            $notification->alertFrequencies()->create([
                'notification_id' => $frequency
            ]);
        }
        }
    }

    private function updateStatus(Notification $notification, Status $status)
    {
        $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);

        /* create an attachemtn in to the alerFrequenct table*/
        $alert = $notification->alert();
        var_dump($alert);

        if ($check || $elapsed_time >= $check_frequency) {
            $resCode = $this->getStatusCode($notification->website_url);

            $this->addStatusToNotification($notification, $status, $resCode);

            $this->sendNotification(
                $notification,
                $status_health,
                $this->getAlertFrequency($notification),
                $resCode,
                $alert
            );

        }
    }
    private function getCheckFrequency(Notification $notification)
    {
        return isset($notification->check_frequency)
        ? intval($notification->check_frequency)
        : $this->default_check_frequency;
    }
     private function getAlertFrequency(Notification $notification)
    {
        return isset($notification->alert_frequency)
        ? intval($notification->alert_frequency)
        : $this->default_check_frequency;
    }


    private function getStatusCode($url)
    {
        try {
            $response = $this->client->get($url, [
                'http_errors' => false
            ]);
            return $response->getStatusCode();
        } catch (\GuzzleHttp\Exception\ConnectException $e) {

        }
    }
}

0 个答案:

没有答案