У меня есть две таблицы (уведомление и 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();
}
}
Он возвращает отметку времени.
Что I'am пытается выполнить в контроллере жрать, чтобы обновить created_at
(поле в alertFrequency
таблицы) с notification_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
? был бы признателен за вашу помощь!
Какова структура 'alerFrequency' таблиц – Gayan
это простая модель с принадлежит многим отношениям :::: функция уведомление общественности() { \t возвращения $ this-> belongsTo ('App \ Notification'); } –