简体   繁体   English

Laravel where 子句基于数据库中值的条件

[英]Laravel where clause based on conditions from value in database

I am building an event reminder page where people can set a reminder for certain events.我正在构建一个事件提醒页面,人们可以在其中为某些事件设置提醒。 There is an option for the user to set the amount of time before they need to be notified.用户可以选择设置在需要通知之前的时间量。 It is stored in notification_time and notification_unit .它存储在notification_timenotification_unit notification_time keeps track of the time before they want to be notified and notification_unit keeps track of the PHP date format in which they selected the time, eg. notification_time跟踪他们想要收到notification_unit之前的时间, notification_unit跟踪他们选择时间的 PHP 日期格式,例如。 i for minutes, H for hours. i为分钟, H为小时。

Eg.例如。 notification_time - 2 and notification_unit - H means they need to be notified 2 hours before. notification_time - 2 和notification_unit - H表示他们需要在 2 小时前收到通知。

I have Cron jobs running in the background for handling the notification.我在后台运行 Cron 作业来处理通知。 This function is being hit once every minute.此功能每分钟被击中一次。

Reminder::where(function ($query) {
    $query->where('event_time', '>=', now()->subMinutes(Carbon::createFromFormat('i', 60)->diffInMinutes() - 1)->format('H:i:s'));
    $query->where('event_time', '<=', now()->subMinutes(Carbon::createFromFormat('i', 60)->diffInMinutes())->format('H:i:s'));
})

In this function, I am hard coding the 'i', 60 while it should be fetched from the database.在这个函数中,我对'i', 60进行了硬编码'i', 60而它应该从数据库中获取。 event_time is also part of the same table event_time也是同一张表的一部分

The table looks something like this -桌子看起来像这样——

id event_time ... notification_unit notification_time created_at updated_at id event_time ... notification_unit notification_time created_at updated_at

Is there any way to solve this issue?有没有办法解决这个问题? Is it possible to do the same logic with SQL instead?是否可以用 SQL 执行相同的逻辑?

A direct answer to this question is not possible.不可能直接回答这个问题。 I found 2 ways to resolve my issue.我找到了 2 种方法来解决我的问题。

First solution第一个解决方案


Mysql has DATEDIFF and DATE_SUB to get timestamp difference and subtract certain intervals from a timestamp. Mysql 有DATEDIFFDATE_SUB来获取时间戳差异并从时间戳中减去某些间隔。 In my case, the function runs every minute.就我而言,该函数每分钟运行一次。 To use them, I have to refactor my database to store the time and unit in seconds in the database.要使用它们,我必须重构我的数据库以在数据库中以秒为unit存储timeunit Then do the calculation.然后进行计算。 I chose not to use this way because both operations are a bit heavy on the server-side since I am running the function every minute.我选择不使用这种方式,因为这两个操作在服务器端都有些繁重,因为我每分钟都在运行该函数。

Second Solution第二种解决方案


This is the solution that I personally did in my case.这是我个人在我的情况下所做的解决方案。 Here I did the calculations while storing it in the database.在这里,我在将其存储在数据库中的同时进行了计算。 Meaning?意义? Let me explain.让我解释。 I created a new table notification_settings which is linked to the reminder (one-one relation).我创建了一个新表notification_settings链接到提醒(一对一关系)。 The table looks like this桌子看起来像这样

id , unit , time , notify_at , repeating , created_at , updated_at id , unit , time , notify_at , repeating , created_at , updated_at

The unit and time columns are only used while displaying the reminder. unittime栏仅在显示提醒时使用。 What I did is, I calculated when to be notified in the notify_at column.我所做的是,我计算了何时在notify_at列中收到通知。 So in the event scheduler, I need to check for the reminders at present (since I am running it every minute).所以在事件调度器中,我需要检查当前的提醒(因为我每分钟都在运行它)。 The repeating column is there to keep track of whether the reminder is repeating or not. repeating列用于跟踪提醒是否重复。 If it is repeating I re-calculate the notify_at column at the time of scheduling.如果重复,我会在调度时重新计算notify_at列。 Once the user is notified notify_at is set to null.一旦通知用户notify_at设置为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM