私は2つのテーブル(通知とalertFrequency)をそれぞれmodels
としています。 1対多の関係があります。できます。私はそれを自動的に更新しようとすると、ここで私は問題を取得します。短い私のモデル通知は:自動的に1対多の関係を更新する
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();
}
}
タイムスタンプを返します。がつがつ食うコントローラで達成しようと何I'am
は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()
することができていますし、正常に動作しています。私はそれを自動的にalerFrequency
テーブルを更新するようにstatus
関数で呼び出す方法を知らないのですか?あなたの助けに感謝します!
それは、多くの関係に属し::::パブリック関数の通知(){ \tリターンます$ this-> belongsToの(「アプリケーション\通知」を持つ単純なモデルである 'alerFrequency'テーブル – Gayan
の構造がどのようなものです); } –